aspose file tools*
The moose likes Servlets and the fly likes Multiple requests to a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple requests to a servlet" Watch "Multiple requests to a servlet" New topic
Author

Multiple requests to a servlet

tom burke
Greenhorn

Joined: Jul 15, 2002
Posts: 4
Hi,
Just want to clear up some confusion on servlets and threads.
Lets say I have a base class called:
public class myBaseServlet extends HttpServlet {
HashMap req_params;
...
}
and then a class:
public class myNiftyServlet extends myBaseServlet{
...
}
I understand (I think), that myNiftyServlet only gets instantiated once, and multiple requests to it will be processed by multiple threads calling the service method, which then calls the doXXX methods.
So in the above example, the req_params would be shared by all threads, and needs to have synchronized access? Or, if I want a unique copy for each thread, it should be declared in the doXXX method?
Any clarification from the enlighted would be appreciated.
Thanks,
Tom
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Tom
You're correct. In a non-distributed servlet the container can use only one instance of a servlet unless the servlet implements the SingleThreadModel interface, then each instance is guaranteed to handle only one request at a time.
In a distributed environment a container will have only one instance of a servlet per servlet declaration per JVM.
Other than using the single thread model I dont know how you'd get each thread to have its own copy of the req_params variable. It is the container itself that creates the threads that access your servlet so one they've gotten to the doXXX method it is too late.
hope that helps


Dave
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Note that you only need synchronized access if your servlets are changing values held in the HashMap or if the values are in some way unique for a particular user.
Bill
tom burke
Greenhorn

Joined: Jul 15, 2002
Posts: 4
Thanks,
This is really about my lack of understanding regarding threads.
So to restate, within each thread, the only variables unique to the thread will be the HttpServletRequest and HttpServletResponse.
My problem is I built a base thread class with some helper methods and variables, and I don't think that will work against multiple threads. I realize the lookup stuff shoudl be fine, but I was creating some objects for the response, and I'm thinking that is wrong.
Thanks.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Tom
If you're creating an instance of the base Thread class you created in one of the doXXX methods then it would be a local variable and each thread that accesses the servlet would get it's own copy of it anyway. Or, were you trying to go the other way and make it shared? If that is the case then just make it an instance variable of the servlet then it'll be shared across the threads that access the servlet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple requests to a servlet