0

I am trying to send response in form of an .xml file from a Java servlet to the client. For that I have written the code below:

if (result) {
    response.setContentType("text/xml");

    PrintWriter out = response.getWriter();

    out.println("<Login>");
    out.println("<status>"+successStatus+"</status>");
    out.println("<username>"+userDTO.getFirstname()+"</username>");
    out.println("<sessionId>"+hSession.getId()+"</sessionId>");
    out.println("<timestamp>"+hSession.getLastAccessedTime()+"</timestamp>");
    out.println("<timeout>"+hSession.getLastAccessedTime()+"</timeout>");
    out.println("</Login>");
}

How can I check on the client whether I get this response or not? Do I need to send the response explicitly or is the above code sufficient to send the response to the client?

Thanks in advance

5
  • 2
    If you want to know whether above code "is sufficient", then test it. Commented Mar 18, 2011 at 6:53
  • Why would you post a question like this without even having tested your pasted code? If tested, post the errors you got, if any, and explain us how you tested it, what the results are, etc., Commented Mar 18, 2011 at 6:58
  • @chiccodoro sir i m new to java and i dont kno much about servlets.i m debugging it but while debugging,after this its not going to client side code.so i m not getting that how to check on client side whether its getting response or not.so please help me sir. Commented Mar 18, 2011 at 7:06
  • @pritsag: It's o.k. to be new to any topic, but nobody can help you if you don't tell us what you expect and what you got instead, and hardly anybody is willing to help you if you accept so few answers to your past questions. Commented Mar 18, 2011 at 7:15
  • @pritsag: If you want to know whether the server response works, simply open the corresponding URL in a web browser and see what you get there. Commented Mar 18, 2011 at 7:17

2 Answers 2

4

Just invoking the servlet's URL will get you to the client side (browser). You don't need to do anything specific.

So, if your servlet is mapped like this,

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.test.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
</servlet-mapping>

Just invoking the URL http://www.example.com/context/MyServlet will get you the XML on the browser!

Sign up to request clarification or add additional context in comments.

Comments

1
public class HelloWorld extends HttpServlet {

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {

    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    out.println("<HTML>");
    out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<BIG>Hello World</BIG>");
    out.println("</BODY></HTML>");
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.