• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet instance question

 
Anuj Anand
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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!!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • 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:
 
Kevin Mukhar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
  • 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..
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic