Two Laptop Bag
The moose likes Servlets and the fly likes How do I convert a response from a servlet to a String? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "How do I convert a response from a servlet to a String?" Watch "How do I convert a response from a servlet to a String?" New topic
Author

How do I convert a response from a servlet to a String?

Jenny Goodwin
Greenhorn

Joined: Mar 07, 2011
Posts: 8
Hi there,

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:



Code in servlet B:



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 in servlet_A, 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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56224
    
  13

You don't. Once you forward, that's the end of the line.

If you need a string from other code, you shouldn't be calling it as a servlet, but as a simple method. Why do you feel you need to go through all the complications of calling a servlet rather than a method just to get s string back?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Jenny Goodwin
Greenhorn

Joined: Mar 07, 2011
Posts: 8
Hi Bear!

Thanks for such a quick reply! In my current design, servlet_B re-directs to a Jsp to allow user entry, and the inputted value is returned to servlet_A. If I can't catch the response from servlet_B, I need to come up with a different architecture!

Bear, thanks for being a hero! I'll mark this as resolved.


Jenny
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56224
    
  13

Once the response is sent to the browser, that's the end of things for sure! There is no way that Servlet A is going to be returned to in the same request cycle.

Perhaps you should back up about 10 steps and let us know what you are actually trying to accomplish, and we can help figure out how to set it up.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How do I convert a response from a servlet to a String?
 
Similar Threads
problems adding new parameters to a request
forward + queryString
Request dispatcher doesn't redirect to the jsp page
Just Finished in OCEWCD Learning
how to connect to database and display query vqlus using proper java/jsp