By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,799 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,799 IT Pros & Developers. It's quick & easy.

Problem in Using Ajax within JSP

P: 13
Is it Possible To return a xml object from a jsp file?
i tried. but i get an error. i have pasted the code & error below.

Code :
<body>
<table width="100%">
<tr>
<td>
<%
String testOne = request.getParameter("HeyYou");
response.setContentType("application/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<testValue>"+testOne+" </testValue>");
%>
</td>
</tr>
</table>
</body>

Exception :

The XML page cannot be displayed
Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
--------------------------------------------------------------------------------

Only one top level element is allowed in an XML document. Error processing resource
Feb 22 '07 #1
Share this Question
Share on Google+
4 Replies

acoder
Expert Mod 15k+
P: 16,027
If the XML is incorrect, it's not going to work. To correct your XML, ask in the XML forum. If you're using Ajax, that's client-side+server-side. You're showing the JSP.

Also, for XML, you can't put it in a table. Form a proper XML document in a JSP file, then from the client-side use an XMLHttpRequest object to request the JSP response. Finally parse the reponse text and display on the client-side.

Perhaps, you should study Ajax/XML further, see here.
Feb 22 '07 #2

P: 13
Dear moderator,
thanx for the reply.
Actually i have worked with ajax. In the jsp, i 'll be writing the script that forms the xmlhttprequest object, and i 'll call a url wich corressponds to java code, where i 'll do my server side activities and then return it to jsp as a xml object. and in the jsp again i 'll parse the responseXML and i'll put it in the form.

But for the application, which i'm trying now is to
i'm passing a jsp file as a url in ajax in a jsp file SampleJsp.jsp.
ex:
var url = "myjsp.jsp?myCode=1";
req.open("GET",url, true);

And in myjsp.jsp,
i'm using the javacode in myjsp.jsp file to return a xmlresponse
ex:
response.setContentType("html/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<testValue>"+request.g etParameter("myCode")+"</testValue>");

Now, im trying to parse the responseXML in SampleJsp.jsp
ex:
var responseValue = responseXML.getElementsByTagName("testValue")[0];

The problem is i'm getting a responseValue as a null object.
My doubt is whether a jsp can return a responseXML .
Feb 22 '07 #3

acoder
Expert Mod 15k+
P: 16,027
The problem is i'm getting a responseValue as a null object.
My doubt is whether a jsp can return a responseXML .
For that, you'll have to ask in the Java forum. All JSP questions are answered in the Java forum.

Why don't you try a simple response first? If that works then you can build on it. If you've already done this, then you need to find out about JSP and XML, but I think it should be able to do it.

Also, one last thing, don't forget that you should add:
Expand|Select|Wrap|Line Numbers
  1. req.send(null);
when making the request to the server. Also, you should have an onreadystatechange event handler for the Ajax to work.
Feb 22 '07 #4

P: 13
Dear Moderator,

Thanx again for replying.

Now i have figured that the problem is in returing a xml object from another jsp file instead of java file. And there is no problem with implementing ajax, coz i'm getting a response object. but it is null.

As per ur suggestion,I 'll post my question in jsp or java group.

- Jagadesh
Feb 23 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.