Sorry to bust up your day with this, but I’ve spent ages trawling the web for an answer and I’m completely stuck!
In a web app, I’m using RequestDispatcher to send a request from servlet_A to servlet_B. In servlet_B, I run some code which generates a simple String value which is returned as a response to servlet_A.
Code in servlet A:
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/servlet_B");
dispatcher.forward(request, response);
/*
How do I catch the response from servlet_B and convert it to a String?
*/
Code in servlet B:
/*
Some other code which creates a String
*/
String result = "someValue";
// Send the response to servlet_A.
response.setContentType("text/plain");
response.setContentLength(result.length());
PrintWriter out = response.getWriter();
out.println(result);
In servlet_A, I need to convert the response from servlet_B into a simple String value, and this is the bit that I’m stuck with. How do I process the response from servlet_B as a simple String in servlet_A?
I think I need to use PrintWriter to parse the response, and I’ve searched the web for an example of this, but all I can find are examples showing how to use PrintWriter to send a response, or to output data to the screen.
I would be very grateful if someone could help me with this.