wood burning stoves 2.0*
The moose likes Servlets and the fly likes Call servlet class from jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Call servlet class from jsp?" Watch "Call servlet class from jsp?" New topic
Author

Call servlet class from jsp?

Sean Michael Hayes
Ranch Hand

Joined: Feb 08, 2012
Posts: 54

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
Rancher

Joined: Sep 21, 2011
Posts: 2408
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

Joined: Feb 08, 2012
Posts: 54

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

Joined: Jan 10, 2002
Posts: 60794
    
  65

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60794
    
  65

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

Joined: Jan 29, 2007
Posts: 313

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.


Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
 
Consider Paul's rocket mass heater.
 
subject: Call servlet class from jsp?
 
Similar Threads
How can I call servlet from jsp without using form
Calling a new JSP form a Spring Controller using Ajax
Implementation of google charts in JSP
DIV Tag Loading Behavior is Erratic
Javacript portion of jsp won't run when refreshed using AJAX?