Solved

How to process HttpServletResponse

Posted on 2004-09-29
10
264 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now