Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call servlet class from jsp?

 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm in the middle of trying to refresh a div on my jsp using AJAX, I'm using jQuery to do this. Problem. The way my project is set up is that when it runs on a server you access the servlet first so it can create data to be sent to the jsp page so it can be accessed using jstl and displayed correctly . I'm wondering how you would go about setting it that accessing the jsp automatically calls the servlet to populate the data variables on the jsp page.

The reason I want this is because the ajax call for the servlet puts the entire html page, head and body tags and all into the div, which I believe, causes errors when I try t run the jQuery in the div. If the above is not possible, how do I go about retrieving the updated div? Google has been no help for my unique problem.

the jsp

 
Tim Moores
Bartender
Posts: 2854
46
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SeanMichael Hayes wrote:I'm wondering how you would go about setting it that accessing the jsp automatically calls the servlet to populate the data variables on the jsp page.

You'd do that by using a backing bean in the JSP. A servlet provides an interface for HTTP calls, but if the call is handled by the JSP, then there's no point in using a servlet.

It doesn't sound as if that would solve the problem of the AJAX call. For that you'd have to write a servlet that does not return a full HTML page, but just the new contents of the DIV.
 
Sean Michael Hayes
Ranch Hand
Posts: 54
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Moores wrote:
It doesn't sound as if that would solve the problem of the AJAX call. For that you'd have to write a servlet that does not return a full HTML page, but just the new contents of the DIV.
he page with fireb

Ah, I just checked my servlet and and viewed the page with firebug and everything seems to be running right. No body and header tags in the div. Still doesn't solve my problem regarding the jQuery not working but its a start, thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64985
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is jQuery "not working"? All you've described as far as problems go is ending up with malformed HTML, and now you've said that that's no longer a problem.

So what is the problem?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64985
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, why would you not just pre-populate the page using JSP and JSTL as you originally described? Ajax should not be used for initial page loading -- just making modifications to the page as a result of events after the page has been loaded.
 
Palak Mathur
Ranch Hand
Posts: 342
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can partially understand the problem statement is as follows. Initially, when you want to open a page, you first hit the servlet which fetches data from db, sets it into session and redirects to a JSP where it is presented. Now, on some event you want to refresh the div on your page. Is this correct? If not, then please help me understand your problem statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic