aspose file tools*
The moose likes Servlets and the fly likes Multiple instances in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple instances in Servlets" Watch "Multiple instances in Servlets" New topic
Author

Multiple instances in Servlets

priya shankar
Ranch Hand

Joined: May 12, 2003
Posts: 51
Hi all

Have a look at the following Prog
public class test extends HttpServlet
{
int a;
public void init(ServletConfig c)throws ServletException
{
super.init(c);
a=0;
}
public void doGet(HtepServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
res.setContentType("text/html");
PrivtWriter pw=res.getWriter();
a=a+1;
pw.println("value of a="+a);
}
public void destroy()
{
a=0;
}
}
When i run this prog it works ...when i cal this prog from a browser at first it shows a=1...at the same time when i opened another browser and call it...i got a=2....
As for i know servlet is going to serve the request by using multithreading concept..if it so two thereads have to be run seperatley and i should get a=1 for each client...

Why i am getting a=2....
2)if servlet serve the request by instance pooling..it is going to give seperate instance to evey request ...so everytime if the init() mdethod must be called...but in my prog it was called only once ..
I am confused ..Pls give me clear solution and idea
thankx
priya
Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
Originally posted by priya shankar:

When i run this prog it works ...when i cal this prog from a browser at first it shows a=1...at the same time when i opened another browser and call it...i got a=2....
As for i know servlet is going to serve the request by using multithreading concept..if it so two thereads have to be run seperatley and i should get a=1 for each client...

Why i am getting a=2....
2)if servlet serve the request by instance pooling..it is going to give seperate instance to evey request ...so everytime if the init() mdethod must be called...but in my prog it was called only once ..

If you want each user to start with a=0, then you should be storing the value in a memory space designed for that; namely the "session."
From the HttpServlet API docs:
Servlets typically run on multithreaded servers, so be aware that a servlet must handle concurrent requests and be careful to synchronize access to shared resources. Shared resources include in-memory data such as instance or class variables and external objects such as files, database connections, and network connections.
(emphasis added)


Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
HL
Greenhorn

Joined: Feb 13, 2002
Posts: 4
Priya,
If you do not want same value of variable a across all threads - you will have to create a variable in service method i.e doget method instead of at class level.
Since same instance of servlet is used in different threads - all class variables are shared among different threads.
Hope this helps.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I think there might be some deeper misunderstandings here about Threads. If not, and it's only a scope problem, then I apologize for talking about what everyone already knows

As for i know servlet is going to serve the request by using multithreading concept
TRUE
if it so two thereads have to be run seperatley
If you have simultaneous requests, then you will have two threads running in your doGet() method, so TRUE.

If you have two requests in sequence, then they run in separate threads in sequence, but this is not really 'multi-threaded'. The servlet is a multi-threaded environment, but in this case there is only one thread of interest.

and i should get a=1 for each client
FALSE

In fact, expecting this is (in my view) expecting what everyone recognizes as a threading 'problem' to be the 'expected result'.

the 2 lines of interest in doGet():
A "valid" execution of the doGet() method by two threads:
THREAD A executes //1
THREAD A executes //2
THREAD B executes //1
THREAD B executes //2

Output: THREAD A : n THREAD B : n +1

This is the "expected" output in this case, because of the scope of the variable 'a' (that is, it is shared between the two threads). And it should also be understood that this expected behaviour is only by luck and good timing.

An "in-valid" (meaning you have threading problems) execution of the doGet() method by two threads:

THREAD A executes //1
THREAD B executes //1
THREAD B executes //2
THREAD A executes //2

Output: THREAD A : n+1 THREAD B : n+1

This would be *unexpected* output, and it's because of two threads wreaking havoc in your non-synchronized method.

The reason I suspected this had more to do with threads than with servlets, was that this is a very common threading issue, and the demonstrated behaviours should be understood as 'good' and 'bad' behaviour in any environment. Again, I apologize if I've just insulted someone.


if servlet serve the request by instance pooling..it is going to give seperate instance to evey request
HARDLY EVER - read the Servlet spec for more details.

...so everytime if the init() mdethod must be called
Even IF the first point was true, this is not true of every REQUEST. Init() would be called 'n' times, where 'n' is the number of servlet instances. Init() is not a constructor, and it's also not called prior to each request. It's called once per servlet instance and then not again.
[ June 15, 2003: Message edited by: Mike Curwen ]
priya shankar
Ranch Hand

Joined: May 12, 2003
Posts: 51
Hi Mike Curwen

Thankx for ur response...
Yeah ,i have misunderstood the Threading ,Pooling concepts...Could u explain me the Both in detail...
If u want u can send some specs to
priya_priyaam@yahoo.co.in..

Expecting ur reply
thanks
priya
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hi priya,

About pooling servlet instances, here is what the spec says about that:So that means the container is going to instantiate a single instance of your servlet class and ALL requests (no matter how many) will be using that single instance. This is why servlets should be coded "thread safe". The part about SingleThreadModel should be ignored, as this is going to be deprecated in the upcoming servlet 2.4 spec.

As for more details on Threads and thread safety, I'd recommend the thread tutorial on java.sun.com

But the short of it (for servlet development) is : AVOID AT ALL COSTS class-level variables. (the variable 'a' in your example)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple instances in Servlets
 
Similar Threads
HFSJ 2nd edition question 43 answer seems ambiguous
Servlet init method behavior
Servlet working
Forwarding data to jsp from servlet
Initparameter not working