Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
    Bookmark Topic Watch Topic
  • New Topic

Request Attributes are thread safe?

 
sharana sharana
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi Ranchers
SCWCD books [ hanumanth.. , wrox both ]says that Request attrbutes is Thread Safe???
lets say Container creates a One instance of a servlet and hence there is one copy of service() method and hence all doxxx() methods.
when mutiple threads try to access the above servlet all gets chance to execute same service() method which delegates services to different doxxx() methods based on request method.
in java Object parameter passing is always a shalow copying [i.e. only reference to the Object is passed], in that case how can two threads one executing in a doget() and other thread executing a doPost() method acting on a same set of request attributes can gurantee Thread safety???

any help here please??
 
boyet silverio
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Report post to moderator
Note that the "request" variable inside the doGet(HttpServletRequest request, HttpServletResponse response) or doPost... method is a local variable hence it is thread-safe.
When a user( or its "flow of control" or thread) enters or uses the doPost (or doGet) method, the user passes a reference of its own "request" object to this method. So there could be many request objects of users out there.
as I understand it, there may be one servlet object used by many users. But not necessarily only one "request" object used by many users as each user has its own request object.
hope this helps.
[ May 25, 2003: Message edited by: boyet silverio ]
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Report post to moderator
I think container will make one request object for every request from client. And that request is valid until the response is commited.
Correct me if i am wrong
daniel
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Report post to moderator
This makes the limited assumtion that all Threads are started and controlled by the Server. If they are, then yes, the Request Object and it's Attributes are Thread Safe. However, Nothing prevents you from starting a second Thread on a request and passing it the Request Object. Since the Request Attributes are then a shared resource by both Threads, it is possible for one to change the same Request Attribute that the other relies on! Therefore, the Request Attributes are NOT Thread safe. The Request Parameters are immutable so, they are Thread Safe. This only holds true for Request Objects that only implement the methods of HttpResquest. If you create your own RequestWrapper or use someone else's RequestWrapper, changes can be added which expose the Parameters as mutable and therefore would not be Thread Safe!
 
    Bookmark Topic Watch Topic
  • New Topic