• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple requests to a servlet

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You may have just won ten million dollars! Or, maybe a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic