Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript <-> Servlet communication

Status
Not open for further replies.

Pesho318i

Programmer
Jun 16, 2006
13
NL
Hi all,

My question may be too fundamental, but I couldn't find a solution so far.
I have a Java servlet deployed in tomcat. Apart from that, I have JavaScript functions which should asynchronously pass information to the Java servlet, so that I can use the servlet functions.

I'd be very grateful if you could give me a hint how to do it.

Have a nice day :),
Pesho
 
Here is one way to do it....

Make a seperate jsp file which takes parameters via URL.
Have the JSP file communicate with the servlet, echo out the data, then have Javascript parse the results and update what you need to.
--------

Example:
I want to keep checking for the number of posts for a specific topic called "AJAX" in our forums.

URL Parameters.
Code:
[blue]
[URL unfurl="true"]http://www.mysite.com/getNumPosts.jsp?title=AJAX[/URL]
[/blue]


JSP File...
Forgive the quickness and lack of data validity checks, but this is all theoretical :)

Lets assume you have a class that can communicate with the database and pull back objects which contain properties about forums.

Code:
[blue]
<%
...
 import things here
...

 String title = request.getParameter("title");
 [green]//Get a new DB object [/green]
 MyDatabaseManager m = new MyDatabaseManager();
 PostProperties z = null;

 [green]//Init posts to 0[/green]
 int posts = 0;
 
 [green]//If not null get the number posts [/green]
 if ( (z=m.getPostProperties(title)!=null)
 { 
   posts = z.getNumberOfPosts();
 }
 System.out.print(posts);
%>
[/blue]

[red]This code will initialize a new database connection, get the post properties, then echo out the number of posts. Results will look like:  0 or 10
[/red]


--------------------------------
I'll assume you know a bit about AJAX connection initialization and jump straight to results parsing.

Remember our URL from above?

Let's assume you have this in a variable called url and you pass it to your ajax call.


Code:
[blue]
function getUpdate()
{
...
var httpRequest = initializeHttpRequest();
	if(httpRequest)
	{
		httpRequest.onreadystatechange = function()
		{
			if (httpRequest.readyState == 4)
    		{	
				var response  =httpRequest.responseText;
[red]parseResults(response);[/red]
			} //end if readystate
		}//End onreadystate
		httpRequest.open("GET", url, true);
		httpRequest.send(null);
}
[/blue]

Notice the parseResults. That's our javascript function that will take care of our results. Lets assume we have a div tag called "MyTag" that will display the results in the page. Afterwards, the function will set a timer to call getUpdate again in 30 seconds to check for the most recent number of posts.

Code:
[blue]
function parseResults(result)
{
   var x = document.getElementById("MyTag");
   x.innerHTML = result;

   [red]setTimeout('getUpdate()',30000);[/red]
}
[/blue]

Now in the page, we need to called it somehow, lets do it on page load....

Code:
[blue]
<body onLoad="getUpdate()">
</body>
[/blue]

So now when the page loads, it will call getUpdate which will call your jsp, pass the results to a javascript function which will update the content somewhere in the page and then set a timer to do it all again in 30 seconds asynchronously.

Wow, crazy post huh?
Hope that helps or at leasts points you in the correct direction!

~Ron









typedef map<GiantX,gold, less<std::shortestpathtogold> > AwesomeMap;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top