Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending and receiving data through jsp and Ajax

 
Shridhar Raghavan
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dhaval J. Patel
Ranch Hand
Posts: 90
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic