File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Life Span of a request. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Life Span of a request." Watch "Life Span of a request." New topic
Author

Life Span of a request.

Thomas Mcfarrow
Ranch Hand

Joined: Jul 09, 2001
Posts: 137
Hello,
If I saved a HttpServletRequest in a session and then tried to do a request.getRequestDispatcher().forward(....) with the request in the session before the session times out. Will I have any problems?
Is there a possibility that the request will be garbage collected by then? If so what are some solutions? Using the HttpServletRequestWrapper?
[ May 23, 2003: Message edited by: Thomas Mcfarrow ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
The servlet engine manages the request and response objects. You can't save them in a session because the engine might re-use them for efficiency sake.
In any case many of the attached resources, such as the output stream will be totally meaningless after the response has been sent and the client connection closed.
What are you trying to accomplish?
Bill
Thomas Mcfarrow
Ranch Hand

Joined: Jul 09, 2001
Posts: 137
Scenario:
You have an HTML page with post (original request) and you get a response from a HTML/Servlet/JSP ignoring original request. Next you need to send a request with values from HTML/Servlet/JSP and the original request to a Servlet/jsp.
Why?:
The code was written/designed this way and I didn't have any control of the design. I just need a way to make this work with minimal code change
One fix is building a query string but that is not how a post should work.
So my next thought is to set the Content-Length header field to the size of the query string and put the query string in the header. I believe thats how a post works. Do you know how do to set the query string in the header?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
I'm not clear on something here - are you writing just some sort of client that has to talk to existing servlet/jsp, writing a servlet/jsp that has to respond to a browser request, or what?
Can you establish a session with the data from the first request?
How about writing hidden variables into a form?
(incidently, content length refers to the body of an HTTP message, not headers)
Bill
Thomas Mcfarrow
Ranch Hand

Joined: Jul 09, 2001
Posts: 137
Hi Bill,
I wish I could give you exact details but unfortunately I can't(company policy). So I came up with something very similar.
Lets say any HTML page trys to send a request (via POST) to a servlet but there is an interceptor servlet of sort that works similar to Form Based Authentication or a survey.
(We will pretend the interceptor was survey type servlet)
The way underlying code works is that after the user fills in the required fields in the survey, the results will be submitted to another servlet. This Servlet will respond either back to the survey form (if a field was missed) or with a redirection to the original requested servlet with a query string (from the original HTML request) attached to the url.
This works great for GETS since there are query strings but not for POSTS. One thing I did was build a query string from paramaters when I got a POST. Even though this worked, it is not acceptable since POST don't behave in this manner.
Deep Breath, the bottom line this has to work with any Servlet/HTML/Jsp page that is sending the original POSTS request. I am not customizing those files. I am concerned with the interceptor layer.
I can't change the original design of the code. I just need to find a way to get POSTS working like they should.
---------------------------
Yes a session is established.
Yes I realize the content-length (my mistake) involves the body of the HTTP Message. Is there a way to set the body of the Http Request Message with the query string? Or how do you build a request programatically?
[ May 24, 2003: Message edited by: Thomas Mcfarrow ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12791
    
    5
Post sends name-value data pairs in the body of the HTTP message.
The best way to understand what goes on with a Post is to intercept the conversation between a browser and server so you can look at the actual headers and body created when a Form is Posted. When I was writing a book on SOAP I wrote a utility to do this, and there are also other utilities for the same purpose.
You can download
my UtilSnoop program (in Java) - its at the bottom of the page.
 
GeeCON Prague 2014
 
subject: Life Span of a request.