Reputation: 1
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.
Upvotes: 0
Views: 2540
Reputation: 1108632
Technically, you're going in the wrong direction as to salvaging the problem. The other servlet which you're forwarding to is clearly tight coupled and its code needs to be refactored into a standalone class which returns the desired data immediately as String, so that the other servlet can write it to the response and that the current servlet can use the standalone class directly instead of invoking a whole other servlet for that first.
Ignoring the smelly bad design, you could solve it by replacing the current response with a HttpServletResponseWrapper
implementation which writes to an internal string buffer instead of the actual response body. Here's a concrete kickoff example which suits your particular functional requirement (noted should be that the actual implementation may be much more complicated than this, you need to take getOutputStream()
and getCharacterEncoding()
into account as well):
final StringWriter buffer = new StringWriter();
request.getRequestDispatcher("/servlet_B").include(request, new HttpServletResponseWrapper(response) {
private PrintWriter writer = new PrintWriter(buffer);
@Override
public PrintWriter getWriter() throws IOException {
return writer;
}
});
String writtenByServletB = buffer.toString();
// ...
Upvotes: 1
Reputation: 6617
I guess all you need is to carry a String from the previous servlet to the next one , then all you have to do is to add an attribute in your request object . so whn you pass the request to the next servlet the request object carries the value set as an attribute
so in the first Servlet you can write
String sendThisValue = "some Important value to be sent";
request.setAttribute("sendThisValue",sendThisValue);
RequestDispatcher dispatcher = context.getRequestDispatcher(“/servlet_B”);
dispatcher.forward(request, response);
then as the control passes to the next servlet you can access the value you set in your previous servlet with th request object by
String gotTheValue = request.getAttribute("sendThisValue");
Upvotes: 0
Reputation: 147
in servlet B I think you can use again dispatcher to get back to servlet A
ServletContext context = getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher(“/servlet_A”);
request.setAttribute("result",result);//the String you want to pass
dispatcher.forward(request, response);
Upvotes: 0