Solved

How to process HttpServletResponse

Posted on 2004-09-29
10
267 Views
Last Modified: 2013-11-24
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
0
Comment
Question by:smita_raut
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 1

Expert Comment

by:frank_van_puffelen
ID: 12179300
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
 
LVL 4

Expert Comment

by:gdrnec
ID: 12179341
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
 
LVL 1

Expert Comment

by:frank_van_puffelen
ID: 12179348
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Expert Comment

by:frank_van_puffelen
ID: 12179359
gdrnec is right by the way.
I keep forgetting that a request dispatcher also gets the response passed as a parameter.
0
 

Author Comment

by:smita_raut
ID: 12179770
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
 
LVL 4

Expert Comment

by:gdrnec
ID: 12179845
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
 
LVL 1

Expert Comment

by:frank_van_puffelen
ID: 12180276
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
 
LVL 2

Expert Comment

by:tbeernot
ID: 12181225
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
 

Accepted Solution

by:
veeresh_khanorkar earned 250 total points
ID: 12224089
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
 
LVL 2

Expert Comment

by:tbeernot
ID: 12224138
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

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question