Hi, I was wondering about the life time of a HttpServletRequest object. I think it is longer than that of a HttpSession object, since a HttpSession is stored in a HttpServletRequest object. Then when is HttpServletRequest destroyed or expire? On the same token, how long is the life span of a HttpServletResponse object? When will it expire?
request and response objects are created by the servlet runner and are provided to servlet coders as parameters on certain (very useful) methods like doGet and doPost. As such (and because the web is stateless) each request and response object are recreated each time the servlet is invoked.
The session is maintained in a different context than these objects, and is only encapsulated by the request object.
I think it's a mistake to link 'containment' with 'life-time'. Just because a request contains a reference to the session, doesn't mean the request lasts longer. In fact, its the opposite.
Joined: Apr 06, 2001
Thank you Mike. So, the scope of a request ends upon return of the service() method?
The scope of the request is over once the particular doXXX method is over.
Request and response objects in servlets are passed as parameters to doXXX methods. Therefore, they are in scope only as long as execution is in those methods.
I should say that the servlet runner may still have these objects after that. They had to come from somewhere right? Somebody passed them to your doXXX method. Maybe they do something to them after the doXXX method is done. I really don't know. But it doesn't matter. As far as you, the servlet writer, is concerned, they are dead after the method ends. [This message has been edited by Mike Curwen (edited May 29, 2001).]