tags:

views:

130

answers:

3

I have two jsp pages, index.jsp and Result.jsp. In index.jsp I have a text box and a Div. When user writes something in the textbox, the text with current date should appears in Div. On keyup event of text box I am sending an Ajax request to Result.jsp. With responseText method everything is working fine. But when I am sending an xml from server and trying to getting that with responseXML method, that is not working with following Result.jsp -

<%@ page import="java.util.Date"%>
<%
    response.setContentType("text/xml");
    Date now = new Date(); 
    StringBuffer sb = new StringBuffer("<?xml version='1.0' ?><root>");
    String name = request.getParameter("name");
    sb.append("<data>");
    sb.append(name + " / " + now);
    sb.append("</data>");
    sb.append("</root>");
    out.print(sb);
%>

In this case responseXML is returning null.

But its working with following Result.jsp -

<%
    response.setContentType("text/xml");
    java.util.Date now = new java.util.Date(); 
    StringBuffer sb = new StringBuffer("<?xml version='1.0' ?><root>");
    String name = request.getParameter("name");
    sb.append("<data>");
    sb.append(name + " / " + now);
    sb.append("</data>");
    sb.append("</root>");
    out.print(sb);
%>

I am not able to understand what is the problem with <%@ page import="java.util.Date"%> in previous case.

A: 

Try using Fiddler or Firebug then inspect the response of the request. Or perhaps compare the value of responseText and responseXML. Check also the response headers if the content-type is correct.

jerjer
A: 

Do you get an error message with the first version (check the logs)? If yes post it.

I'm not that familiar with JSP but I guess the missing space at the end

Date"%> vs Date" %>

doesn't matter.

Did you try importing the whole package instead of a specific class?

<%@ page import="java.util.*" %>

Did you try calling the Result.jsp directly with your browser? Does it return an xml snippet with the second version but not with the first one?

jitter
When I have call Result.jsp directly from browser, it returns correct xml. I have tried Date"%> and Date" %> but got no effect. Also, there is no error message in logs.
Saurabh
Same result with <%@ page import="java.util.*" %> :(
Saurabh
A: 

Great. Got that worked. There was a new line character in the beginning of xml. I have cleared out object before writing the xml into it and it works.

<%@ page import="java.util.Date"%>
<%
    response.setContentType("text/xml");
    Date now = new Date(); 
    StringBuffer sb = new StringBuffer("<?xml version='1.0' ?><root>");
    String name = request.getParameter("name");
    sb.append("<data>");
    sb.append(name + " / " + now);
    sb.append("</data>");
    sb.append("</root>");
    out.clear();
    out.print(sb);
%>
Saurabh