Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to process HttpServletResponse

Posted on 2004-09-29
10
Medium Priority
?
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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 750 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

721 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