This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

request life span

 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mike. So, the scope of a request ends upon return of the service() method?
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic