This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Doubt on servlet request lifecycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Doubt on servlet request lifecycle" Watch "Doubt on servlet request lifecycle" New topic
Author

Doubt on servlet request lifecycle

Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
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?


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

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

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
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

Joined: Sep 19, 2008
Posts: 385
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on servlet request lifecycle
 
Similar Threads
send response to calling servlet
Forward between Web Apps - How To?
Servlet Multithreading
ThreadSafety of HttpServletRequest & HttpServletResponse objects
whats the difference between forward and redirect?