Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple requests to a servlet

 
tom burke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic