• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jenny Goodwin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jenny Goodwin
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65120
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic