• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Doubt on servlet request lifecycle  RSS feed

 
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 Ubuntu VI Editor
  • 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.
 
Author and ninkuma
Marshal
Posts: 66806
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!