aspose file tools*
The moose likes JSP and the fly likes Sending and receiving data through jsp and Ajax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Sending and receiving data through jsp and Ajax" Watch "Sending and receiving data through jsp and Ajax" New topic
Author

Sending and receiving data through jsp and Ajax

Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71
Hi,

Through an ajax call to a JSP i wish to send across information. In the JSP, i invoke reponse.getWriter().write(someCommaDelimitedString).

However when i try to access the data in the call back in ajax through xmlHttp.responseText, the responseText contains some <html> code which i did not include. How do i access only my data or send across data in an easy format to retrieve.
Dhaval J. Patel
Ranch Hand

Joined: Mar 10, 2011
Posts: 90

Shridhar Raghavan wrote: Through an ajax call to a JSP i wish to send across information. In the JSP, i invoke reponse.getWriter().write(someCommaDelimitedString).

Hi Shridhar,
Can you please shed some more light on what you are trying to do?
Shridhar Raghavan
Ranch Hand

Joined: May 27, 2010
Posts: 71
My JSP code is something like
<>

The ajax code in the callback ie the function that receives the response from the server does something like this



When i inspect the response in the browser, the response also contains some html code at the fag end after my payload.
When i looked at the servlet code in the work directory, the servlet code compiled adds some "<head><html>" code to the output stream.

I however have not added such code. Any reasons?
Dhaval J. Patel
Ranch Hand

Joined: Mar 10, 2011
Posts: 90

Please post the code where you are creating the ajax request.
Something like

requestVariable.open("GET","???", true);

Are you directly refering to a jsp there (in place of the "???" in the code above ) ??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Shridhar Raghavan wrote:My JSP code is something like
<>

This code is beyond ridiculous. The whole purpose of a JSP is to allow you to specify non-dynamic values as template text. The proper equivalent is simply:

Also, this is almost 2012. Java scriptlets in a JSP have been discredited for 10 years now. 10 years! A decade. Do you not think that it's time to start using modern techniques such as the JSTL and EL?

Finally, whatever is written to the response is what the Ajax request will receive. Something in your execution chain, be it the servlet, a JSP, or perhaps a filter is adding that text.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dhaval J. Patel
Ranch Hand

Joined: Mar 10, 2011
Posts: 90

Exactly!! Jsps are not meant for that. JSPs are meant only for presentation. If you want to make an ajax request, make it to a servlet instead.

And if you are wondering where the html comes from here are two possible explanations.

1) your server turns the jsp into a servlet, which has a normal service method. The servlet then does something like getting a printwriter and sending across all the html code and also executes the code you wrote in the scriptlet. Hence no matter what you do, the html code will be there in your output. NEVER NEVER NEVER use scriptlets in JSPs. (You might get fired for that someday, as Head First book says)

2) if you are calling the jsp directly from the ajax request (I dont know why you would do that). As in requestVariable.open("GET","blabla.jsp",true) then the html is most probably about the error page for 404 error produced by tomcat.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Dhaval J. Patel wrote:Exactly!! Jsps are not meant for that. JSPs are meant only for presentation. If you want to make an ajax request, make it to a servlet instead.

That actually depends. If the response of the Ajax request is meant to be pre-formatted HTML, then a JSP is appropriate. Otherwise, not so much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending and receiving data through jsp and Ajax
 
Similar Threads
outputing javascript from a servlet
ArrayList and Ajax
How to connect jdbc with javascript
Struts with AJAX pagination
How to pass Java Script variable to JSTL