• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

service() method

 
Harsha Vardhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is service method called for every request ???
-Thanks
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one servlet instance per registered servlet. Normally, when concurrent requests are handled by the web server, threads are spawned which executes the service() method of the servlet. Instance variables are not thread safe, but local variables within the service() method are. The request object is also "localized" per thread.
According to the Servlet specs, when a servlet implements the SingleThreadModel interface (which is an empty "tag" interface) and concurrent requests are handled by the web server, the container is supposed to create a new servlet instance per request. It is possible that the container will pool these servlet instances. it is also possible that the container will serialize, i.e. queue, the requests per instance. It is possible that the container implementation is a combination of these. The upshot of all this is that service() is now thread-safe.
 
Harsha Vardhan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for a detailed explanantion Anthony...
really appreciate that
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome
 
Yu Chen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
===========
According to the Servlet specs, when a servlet implements the SingleThreadModel interface (which is an empty "tag" interface) and concurrent requests are handled by the web server, the container is supposed to create a new servlet instance per request. It is possible that the container will pool these servlet instances.
===========
I don't think the container will create one new servlet per request, this is way too expensive. The Servlet Spec says,
<B><i>
"The user of the SingleThreadModel interface gurantees that only one thread at a time will execute in a given servlet instance's service method. It is important to note that this gurantee only applies to each servlet instance, since the container may choose to pool such objects."
</I></B>
-yu chen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic