How to process HttpServletResponse

Hi Experts,

I have a servlet that needs to call doGet() of another servlet (parent). After this, I need to extract data from the HttpServletResponse object that the parent servlet created and process it further to create another HttpServletResponse object. Is there any way to do that?

Thanks,
Smita
smita_rautAsked:
Who is Participating?
 
veeresh_khanorkarConnect With a Mentor Commented:
Are you looking for URL/URLConnection classes?
From what I understand from your question, you need to make a request to some other servlet and process the response you get. If that is what you want, you can use the above mentioned classes.
0
 
frank_van_puffelenCommented:
It sounds like you're trying to implement filtering functionality. If that's the case, be aware that as of version 2.3 of the servlet specification you can implement Filters without needing to call doGet or anything like that.
More information about the interface that you need to implement can be found at http://java.sun.com/products/servlet/Filters.html, with an API reference at http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/Filter.html.
0
 
gdrnecCommented:
Yes,

In fact someone thought of this already. The class you are looking for is the Request Dispatcher

RequestDispatcher rd;
rd = req.getRequestDispatcher("pathToServlet");
rd.forward(req, res);

This forwards you current request and response to another servlet.

Geoff
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
frank_van_puffelenCommented:
It took me some time to look up the link to the J2EE tutorial, but here it is: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Servlets8.html#wp64572
0
 
frank_van_puffelenCommented:
gdrnec is right by the way.
I keep forgetting that a request dispatcher also gets the response passed as a parameter.
0
 
smita_rautAuthor Commented:
I have gone through the RequestDispatcher. Using this, I can either forward to another servlet (but then I lose the control on response), or use the include method (here again I cannot modify the response I get from other servlet). So this does not serve the purpose.

I read in brief about the Filter too, but couldnt find a way to actually modify the ServletResponse.

I get the ServletResponse in the following format-
Content-type = text/plain
xyz:number:xyz

And I need the "number" portion from this response.
0
 
gdrnecCommented:
I may be wrong here but you seem to be needing to pass data between a number of cooperating objects (servlets). I think the right way to do this is not by using the request/response objects but to share the data in the HttpSession that both servlets (parent and child) have access to. The HttpSession can be used like a shared data store for the lifetime of the session.

Is this at all helpfull?

Geoff
0
 
frank_van_puffelenCommented:
If you look at the link I sent you should find some additional information about having the filter basically replace the output stream that is used by the other servlet. If you do that, you can replace anything the servlets write with anything you want.

Geoff's suggestion could also solve your problem if you're indeed just looking for a way to pass information between the two servlet. If the information is only needed during the handling of the single request, you should consider storing it in the request object as an attribute instead of in a session. The attributes in the request are discarded at the end of the request, so they're much cheaper than objects in the session .
You can set an attribute in the request with:
   request.setAttribute("xyz:numer", "xyz");
If you want to number to get written into the response as a header, be sure to call setHeader on the response object.
0
 
tbeernotCommented:
I think you need to wrap the response in a wrapper and send that to the parent (a convenient ResponseWrapper to start with, is present in de spec). In the wrapper replace the printstream with one of your own, so the data is not actually being written out to the socket. In the wrapper you analyse the output.
0
 
tbeernotCommented:
Interestingly enough probably everyone of the respondents knew the accepted answer, however the "(parent)" in the question left us all standing on the wrong foot. Good catch veeresh.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.