File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlets and Threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets and Threading" Watch "Servlets and Threading" New topic

Servlets and Threading

Brian Moakley

Joined: Apr 12, 2007
Posts: 11
So I'm new to servlets and relatively new to Java as well. I'm currently reading Head First Servlets, and I can't seem to wrap my head around the threading issue.

The book explains that there is only one instance of a servlet, and each request spawns a different thread. So here's where I'm hitting my head against the wall.

If Tomcat creates three different threads, passing three different request and response objects ... how can that one object manage those three different objects if it is the same instance? Logic dictates that there would be three different servlet instances to handle those three different request/response objects yet the book it quite clear that it is only instance of the servlet servicing multiples requests. What is to stop one thread for setting the response object with a newer response object midway through processing?

Any guidance would be appreciated. Thanks!
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257

who manages the Servlet? Its the Container(Server).

Now suppose I have one Servlet A. which has a service(HttpRequest req,HttpResponse) method. The container creates one object of this Servlet and keeps it with it. When a new Request comes from the browser to the server and this request maps to Servlet A(as per the web.xml), then the container creates a new Thread and new HttpRequest & HttpResponse object and passes the call to this Servlet's service method. For every new request a new Thread(number of thread for a server is pre-configured) is created by the container and service method of the same servlet object is invoked.

I hope this claries your doubt.

Amit Tank
Linked In
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

This is why Servlets need to be free of request state. Ideally they should be stateless, but as long as the state can be shared by all threads there usually isn't an issue.

Consider the following class...

When an instance is created, it doesn't matter how many threads call printValue() on the same instance, they all end up printing the same value.

Now we allow the print method to accept different values and remove the state

Again if you cretae a single instance of the class and multiple threads call printValue(), the value they pass to the method is the value that gets printed.

The difference between the two classes is that while both allow multiple threads to access a single instance, the first prints a single value while the second can print any value passed to it. The 'value' parameter represents the context that the printValue method uses when it executes.

To complete the example, comsider what happens when the second class tries to maintain state too...

When accessed by multiple threads, each one sets the state on the UnSafeStatefulClass instance to their own value, and the instance value is printed.
If thread one sets the state, but thread two changes the value before thread one prints the value, then theads one and two both print the same value. Not what was anticipated.

Stepping back to Servlets, the request and response objects passed to the method are safe as long as they are in the method. If you try to retain them as state in the Servlet then you create the situation where multiple threads could accidentally get confused. This is not a good thing.

The benefit is that we don't need to create a Servlet instance for every request (thread tend to come from pools rather than creating a new one every timme too), the down side is that you need to be careful about keeping each request separated.
Brian Moakley

Joined: Apr 12, 2007
Posts: 11
Thank you for the very thoughtful response ... I now understand the solution and my head doesn't hurt so much. Cheers!
I agree. Here's the link:
subject: Servlets and Threading
It's not a secret anymore!