This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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).
Can you please shed some more light on what you are trying to do?
Joined: May 27, 2010
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.
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.
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.