wood burning stoves*
The moose likes Servlets and the fly likes Some Clarification about servlet ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Some Clarification about servlet ??" Watch "Some Clarification about servlet ??" New topic
Author

Some Clarification about servlet ??

Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,
I know that servlet is by default multithreaded but i am little bit confused that there is usually only one instance of our servlet in the server and one new thread for each request.That means if there is 50 request then there is 50 threads and only one instance of servlet is giving service to all 50 requests. Am I right ?
Another question is I want to define a variable in my servlet and initiallized it to '0' and i want that whenever a new request comes that request should find the value of that variable '0'.Because every request will do some calculation with that variable with their specific requirements.For that if i defined that variable and initiallized it to '0' in init method of servlet using 'static' key word, Is it work ?
Thanks & Regards
Bikash
Gowrishankar Mudaliar
Ranch Hand

Joined: Oct 20, 2001
Posts: 39
A simple instance variable initialized to 0 should work for you.
You do not need a static qualifier here.
Mouli Vidiyala
Greenhorn

Joined: Mar 25, 2004
Posts: 2
Bikash,
No need to use static just for this purpose.
Define an instance variable and initialize it to 0.
If you do not want the program to change the value, while execution, you can qualify with final keyword.
-Mouli.
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi all,
Thanks for the reply of my second question but still i am very much eager to know the answer of my first question.I am very eager to know that my understanding about servlet is right or worng .
Thanks & Regards
Bikash
[ March 26, 2004: Message edited by: Bikash Paul ]
Sajee Joseph
Ranch Hand

Joined: Jan 17, 2001
Posts: 200
What i understand is that Servlet Specification doesnt specify how many servlet instances may be created. I think this is left to the vendors.
I am not sure though. Please go thru the Servlet Specification
Guys... reply !!!
Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64
Hi Paul,
You can specify, only one thread can access an instance of a servlet at a time by making the servlet class to implement the SingleThread interface. Now for each new request, a new servlet instance is created.
For normal servlets,the number of instances created can be specified and is normally dependent on vendor of the container.The container can create more instances for the servlet for pooling or for load balancing etc.
Regards
Afroz Ahmed.


The value of an idea lies in the usage of it.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
For normal servlets,the number of instances created can be specified and is normally dependent on vendor of the container.The container can create more instances for the servlet for pooling or for load balancing etc.

My reading of the servlet API is that a servlet container/servlet context is expected to have only one instance of a given servlet - except in the SingleThreadModel case.
Load balancing would involve more than one servlet container/servlet context.
Bill
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi,
What I understood from servlet API is that in case of Normal servlet(not SingleThreadModel case)only one instance of servlet is created and different request access that instance of servlet through different threads.
Iam not going into that topic whether the number of instances created can be specified by container vendor or not. I am talking about in normal case how many instance create when one servlet loaded by server.And if number of request increase then what happens ? Is that single instance give service to all request through different threads ?
Thanks & Regards
Bikash
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

question: how many servlet instances are present within a single container?
answer: one.

you asked further: "And if number of request increase then what happens ? Is that single instance give service to all request through different threads ?"

first answer: new request processing threads are created, up to some number... (more later).
second answer: yes.

the more: For example, a Tomcat container can be configured to accept a maximum number of requests. When this maximum is reached, and you also exhaust the queue setting maximum (the number of requests to queue and wait for 'real' processing threads)... you start getting 'busy' responses from the container.

Another container (like iPlanet) has a policy of 'never refuse a request'. That means that you will effectively have only the OS-imposed limits for creation of new threads to constrain your maximum number of requests.
Bikash Paul
Ranch Hand

Joined: Dec 04, 2001
Posts: 342
Hi Mike,
Thanks for ur reply.Now it is clear to me.
Thanks & Regards
Bikash
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Sajee Joseph:
What i understand is that Servlet Specification doesnt specify how many servlet instances may be created. I think this is left to the vendors.
I am not sure though. Please go thru the Servlet Specification
Guys... reply !!!

This is correct. You cannot rely on instance variables being shared between requests.
Usually they will be shared between all requests unless the application is running in a clustered environment but servers are free to create multiple instances of a servlet class if they want to (whether any actually do this I don't know).


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Some Clarification about servlet ??
 
Similar Threads
out -- in new Thread
JSP Thread question
How servlet process multiple request.
Servlets + Threads
Servlet Concepts