Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on servlet request lifecycle

 
Siva Masilamani
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?

 
Christophe Verré
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic