0

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.

  • 1
    Is it really necessary to have a Servlet B? Can't you use a simple class which will receive the HttpServletRequest and HttpServletRequest as parameters, do the work and then return to the Servlet A? – Diogo Moreira Feb 01 '13 at 18:08
  • Hi Diogo, and thanks for your contribution. I’m afraid so. When servlet_B receives a request from servlet_A, the request is re-directed to a jsp for user input, so I’m stuck with using a servlet. – Jenny_Goodwin Feb 01 '13 at 18:36
  • What I meant was: From what I understood, your servlet B exists just because you have to do some processing with request and response objects and then return to the servlet A, right? If so, you could change the servlet B into an class that will do the job for you, and it only will be instantiated when you need. – Diogo Moreira Feb 01 '13 at 18:50
  • Hi Diogo, and thanks again. If it was just a question of processing data, I could use an ordinary java class. However, when the request arrives in servlet_B, the request needs to be re-directed to a java server page for user input, and I think I need a servlet for that… – Jenny_Goodwin Feb 01 '13 at 19:00
  • I understood that you redirect from the Servlet B to A after processing data... Sorry! (: – Diogo Moreira Feb 01 '13 at 19:04

3 Answers3

1

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();
// ...
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

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);
PetMarion
  • 147
  • 3
  • 14
  • Interesting! I hadn’t thought of that… There may be some issues with this approach – in the final implementation, servlet_B will be imported into the web-app as part of a separate WAR file, and will have no knowledge of the address of servlet_A, which would make request forwarding a bit of an issue. Interesting idea, but this approach may cause further problems down the line, and I think I’m still stuck with having to process the response from servlet_B in the normal way. But hey, thanks for taking an interest… – Jenny_Goodwin Feb 01 '13 at 18:21
  • how are you ccessing the servlets , i mean what is the step of the servlet calling ??? – Hussain Akhtar Wahid 'Ghouri' Feb 01 '13 at 21:19
0

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");
  • Hi Hussain. If I understand you correctly, you’re suggesting how I might add data to the request in servlet_A before it is sent to servlet_B. Thanks for helping out, but I’m afraid that’s not the problem. The problem is how to process the response in servlet_A after it has been returned from servlet_B. – Jenny_Goodwin Feb 01 '13 at 18:47