File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ThreadSafety of HttpServletRequest & HttpServletResponse objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "ThreadSafety of HttpServletRequest & HttpServletResponse objects" Watch "ThreadSafety of HttpServletRequest & HttpServletResponse objects" New topic
Author

ThreadSafety of HttpServletRequest & HttpServletResponse objects

Sai Patnala
Ranch Hand

Joined: Jan 09, 2002
Posts: 60
Some mock exams say that HttpServletRequest and HttpServletResponse objects are thread safe.
According to specification, implemenations of request, response objects are not guaranteed to be thread safe.

Am i missing something?


SCJP2, SCWCD 1.4
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
HI

I am preparing for SCWCD and came across the same question.

Did you find out the solution?

If so,could you please explain?


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Thread safe are (when we do not use SingleThreadModel):
- request, response parameters, local parameters (inside doXXX() method)

Thread safe are (when we use SingleThreadModel):
- request, response parameters, local parameters (inside doXXX() method), instance parameters (inside servlet class)


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
This is what the specification syas about the request and response object.
According to specification, implemenations of request, response objects are not guaranteed to be thread safe



I have no doubt about other container managed objects but this one is confusing me...
So could you please explain this?(Why the specification says so?)
amarshi mohanty
Ranch Hand

Joined: Jul 01, 2008
Posts: 110
Hi,
I hope this url will give a clear picture:
http://book.javanb.com/servlets-and-javaserver-pages-the-j2ee_technology-web-tier/0321136497_ch09lev1sec3.html#PLID2


Thanks
Amarshi


SCJP (97%), SCWCD (95%))
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Amarish

I was going through that link but still confused with the explanation.

It also says that the method parameters and local variables are threadsafe.

I don't think method parameters are thread safe.Say if i have an instance variable and passing the reference to the method called by a thread then how come the method parameters are thread safe because it is pointing towards my instance variable which could be accessed by different thread at the same time.

The specification says somthing like this
Implementations of the request and response objects are not guaranteed to be thread
safe. This means that they should only be used within the scope of the request handling
thread.
References to the request and response objects should not be given to objects
executing in other threads as the resulting behavior may be nondeterministic. If
the thread created by the application uses the container-managed objects, such as
the request or response object, those objects must be accessed only within the
servlet’s service life cycle and such thread itself should have a life cycle within
the life cycle of the servlet’s service method because accessing those objects
after the service method ends may cause undeterministic problems. Be aware
that the request and response objects are not thread safe. If those objects were
accessed in the multiple threads, the access should be synchronized or be done
through the wrapper to add the thread safety, for instance, synchronizing the call
of the methods to access the request attribute, or using a local output stream for
the response object within a thread.


Could you please explain what the specification mean by that?

Also it says that the reference to those objects should not be given outside the servlet's service method.

But what happens if we forward the request using RequestDispatcher in which case the request created for Servlet1 is given to the Servlet2 and the servlet1 service method is completed but still Servlet2 has reference to those objects.

I checked this using the hashcode method on the request object and they both returns the same value which mean i believe the request object in Servlet2 is referring to request object that was created for Servlet1.

please explain
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ThreadSafety of HttpServletRequest & HttpServletResponse objects