Link to home
Start Free TrialLog in
Avatar of smita_raut
smita_raut

asked on

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
Avatar of frank_van_puffelen
frank_van_puffelen

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.
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
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
gdrnec is right by the way.
I keep forgetting that a request dispatcher also gets the response passed as a parameter.
Avatar of smita_raut

ASKER

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.
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
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of veeresh_khanorkar
veeresh_khanorkar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.