Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Doubt on servlet request lifecycle

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

i am preparing for SCWCD and was going through Servlet specification where i saw the following content about the request/response life cycle:

Each request object is valid only within the scope of a servlet’s service method, or within the scope of a filter’s doFilter method. Containers commonly recycle request objects in order to avoid the performance overhead of request object creation. The developer must be aware that maintaining references to request objects outside the scope described above is not recommended as it may have indeterminate results.



Let say i am sending request to Servlet1,so the container creates request and response object and pass the reference through service method of Servlet1.
Then this servlet uses the request dispatcher which forwards the request and reponse to the another servlet Servlet2 which do some complex computation.

Now the service method for Servlet1 is completed.So now as per the spec,the reference for request and response in the Servlet2 is not appropriate as it is outside the scope of service method of Servlet1?

Also i have seen many codes that uses forward to forward the request to different sources ,so does it mean that all the codes are vulnarable to the problem described inthe specification?

So can i assume that when we use forward from the request dispatcher,we are always prone to the problem mentioned inthe specification?

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So can i assume that when we use forward from the request dispatcher,we are always prone to the problem mentioned inthe specification?


I don't think so. Forwarding to another servlet will tell the container not to drop the request and response references. It will reuse them and pass them to Servlet2. I think the problem mentioned in the spec is if you decide to store the request and response references somewhere, like in a cache.
 
Sheriff
Posts: 67405
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, during a forward the container knows that the request and response instances are still in play and will not try to re-use them
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:No, during a forward the container knows that the request and response instances are still in play and will not try to re-use them



So that means when we forward the request,we are prone to the problem mentioned in the specification?

Please explain.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI

The specification also says that

The Container Provider should ensure that the dispatch of the request to a
target servlet occurs in the same thread of the same JVM as the original request.



So what happened to the request object inthe forwarded Servlet?
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic