• 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

servlet instance question

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do we mean sharing of servlet instance variables. If anybody can explain with the help of example it will be great. Also what is a single thread model of servlet.....Can anybody explain that with the help of example too!!
 
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
Normally there is only one instance of a servlet - the servlet engine uses a different Thread for each request so instance variables may be accessible by more than one request at any given time. Using instance variables can go on working for a long time and then cause weird problems.
Variables that are created inside a method are only visible to the Thread executing that method, they can't be modified by another request.
Bill

------------------
author of:
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An instance variable is also often known as a class member variable.
public class MyServlet extends HttpServlet {
int x; //x is a member variable
public void doPost(HttpServletRequest req,
HttpServletResponse res)
{
//...
}
}
When the servlet container creates an instance of MyServlet, then that instance has an instance variable x. Now assume a request comes from a client; the servlet container creates a thread and that thread enters the doPost method of the instance. This thread has access to the variable x. If another request thread enters doPost at the SAME time, it also has access to the same variable x. In other words, instance variables are shared by the threads. If one thread makes a change to x, the other thread will see that same change.
If, on the other hand, x is a method variable:
public class MyServlet extends HttpServlet {
public void doPost(HttpServletRequest req,
HttpServletResponse res)
{
int x = 0;
//...
}
}
then each thread gets ITS OWN copy of x. If one thread makes a change to its copy of x, the other thread does not see this change, and vice versa. Method variables are NOT shared between threads.
 
Anuj Anand
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does it mean that if we use a synchronized doPost or doGet method
then only when one request gets over will the other request execute the doGet method because it has only one instance..
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two main reasons not to use instance (member) variables in a servlet.
One, as discussed above is because the servlet container is free to run more than one request thread through the servlet at once. You are right that this case may be preventable by using synchronized doGet and doPost methods, but that is equivalent to marking the servlet as "implements SingleThreadModel", except that it gives the servlet container no idea that it is doing it, which could lead to all sorts of threading and resource utilization problems.
The other reason is that the servlet container is equally free to create as many instances of the servlet as it likes, which may be more than one. So you can never rely on an instance variable value set in one request to still be there in another. The next request may be routed to a completely different servlet, maybe even in another virtual machine or on another machine.
The right places to store information which should persist between requests are in the ServletContext, in the Session, or in some external data store. All these places should be accessible from any thread in any servlet instance without conflict problems.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic