Setting a string in a body of httpResponse

You simply need to get the output stream (or output writer) of the servlet response, and write to that. See ServletResponse.getOutputStream() and ServletResponse.getWriter() for more details.

(Or simply read any servlet tutorial - without the ability to include data in response bodies, servlets would be pretty useless :)


You can write the XML directly to the response as follows:

This example uses a ServletResponse.getWriter(), which is a PrintWriter to write a String to the response.

String responseToClient= "<tdcp><cmd><ack cmd=”Init”><panelistid>3849303</panelistid></ack></cmd></tdcp>";

httpServletResponse.setStatus(HttpServletResponse.SC_OK);
httpServletResponse.getWriter().write(responseToClient);
httpServletResponse.getWriter().flush();

Tags:

Java

Servlets