Problem in Using Ajax within JSP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JagPeram
    New Member
    • Jan 2007
    • 13

    Problem in Using Ajax within JSP

    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.getPara meter("HeyYou") ;
    response.setCon tentType("appli cation/xml");
    response.setHea der("Cache-Control", "no-cache");
    response.getWri ter().write("<t estValue>"+test One+"</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
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    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.

    Comment

    • JagPeram
      New Member
      • Jan 2007
      • 13

      #3
      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?myCo de=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.setCon tentType("html/xml");
      response.setHea der("Cache-Control", "no-cache");
      response.getWri ter().write("<t estValue>"+requ est.getParamete r("myCode")+" </testValue>");

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

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

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by JagPeram
        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:
        Code:
        req.send(null);
        when making the request to the server. Also, you should have an onreadystatecha nge event handler for the Ajax to work.

        Comment

        • JagPeram
          New Member
          • Jan 2007
          • 13

          #5
          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

          Comment

          Working...