Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes servlet instance question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet instance question" Watch "servlet instance question" New topic
Author

servlet instance question

Anuj Anand
Ranch Hand

Joined: Nov 18, 2000
Posts: 96
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

Joined: Mar 22, 2000
Posts: 12806
    
    5
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

Joined: Nov 28, 2000
Posts: 83
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

Joined: Nov 18, 2000
Posts: 96
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

Joined: Jan 07, 1999
Posts: 6920
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.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
 
subject: servlet instance question