wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlets Basic Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets Basic Question" Watch "Servlets Basic Question" New topic
Author

Servlets Basic Question

Raja Sagar Panamgipalli
Ranch Hand

Joined: Aug 13, 2003
Posts: 109
Hi Guys,
I recently figured out something missing in my understanding of servlets.......

Does the servlet container create seperate instances of each servlet i.e...as the requests comein it(ServletContainer) starts creating objects of say a servlet called HelloWorldServlet i.e instances of HelloWorldServlet.

After processing a request it stacks these instances say in a pool. and services the clients when a similar request comes.-----IS IT
[OR]
There is only one servlet say HelloworldServlet and the server creates a new Thread that is listening to port 8080 (say) for every client that is
requesting the servlet.And passes the req and res object to the service method. And at anypoint of time there is only one servlet ..and its all inside Servlet's service method...isit.....
And when we say instance pooling we mean instances of several other servlets....one of each Servlet.

Guys give me links to some excellent resources....i even went thru the specs2.2..couldn't really figure out..

Thank Q

regards
Sagar








SCJP 1.4<br />SCBCD 1.3
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Not usually.
On first request (or on application load depending on settings) a single servlet instance is created.
For each request a new thread is started in this instance.

Servers are free to create more instances to cope with heavy load for example, but this is not guaranteed.


42
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
The idea that servers are free to create multiple instances is bogus.
Servlet 2.3 API section SRV2.2 "Number of Instances" says
For a servlet not hosted in a distributed environment (the default), the servlet
container must use only one instance per servlet declaration.

Anyway, it would not do a thing to cope with heavy load since any number of Threads can be using a given instance without interference.
Bill
Raja Sagar Panamgipalli
Ranch Hand

Joined: Aug 13, 2003
Posts: 109
Hi Williams,

I didn't get what u meant by the following...can you please elaborate...
Anyway, it would not do a thing to cope with heavy load since any number of Threads can be using a given instance without interference.


Thank Q...

Regards
Sagar.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61447
    
  67

didn't get what u meant by the following...


Only one thread can execute at a time. Whether that thread is executing a single instance of the servlet or multiple will make no difference in performance.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Divij Mahajan
Greenhorn

Joined: Jul 26, 2004
Posts: 8
Hi Folks,

My understanding is that under **normal** circumstances the server creates only one instance of a servlet but uses multiple threads to service clients requests. i.e the init method is called only once and all subsequent client requests are passed to the service method.

On the other hand though, if you want to create an instance pool, you implement the SingleThreadModel interface which ensures that the server creates a new instance for each request. This is generally done to overcome the syncronization issues associated with threads.

Please correct me if I am wrong.

-Divij



I recently figured out something missing in my understanding of servlets.......

Does the servlet container create seperate instances of each servlet i.e...as the requests comein it(ServletContainer) starts creating objects of say a servlet called HelloWorldServlet i.e instances of HelloWorldServlet.

After processing a request it stacks these instances say in a pool. and services the clients when a similar request comes.-----IS IT
[OR]
There is only one servlet say HelloworldServlet and the server creates a new Thread that is listening to port 8080 (say) for every client that is
requesting the servlet.And passes the req and res object to the service method. And at anypoint of time there is only one servlet ..and its all inside Servlet's service method...isit.....
And when we say instance pooling we mean instances of several other servlets....one of each Servlet.

Guys give me links to some excellent resources....i even went thru the specs2.2..couldn't really figure out..


-Divij<br />---------------------------------<br />Weinberg's Law: If builders built buildings the way programmers wrote programs, then the first woodpecker that came along would destroy civilization.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
This is generally done to overcome the syncronization issues associated with threads.

To be brief - no
You overcome the "synchronization issues" by never using servlet instance variables to hold user specific or otherwise changable data. Instead, you make use of local variables and the session management capabilities provided for free by the servlet container.
Lets say you have this fragment of code in a doPost method:

String userID = request.getParameter("userid");

Because userID is a local variable, any number of request Threads can be executing doPost, and each will have its own copy of the String variable userID, each pointing to a different String object. If userID was an instance variable of the servlet class, each Thread would see the same String because there is only one servlet instance.
Bill
[ July 26, 2004: Message edited by: William Brogden ]
Divij Mahajan
Greenhorn

Joined: Jul 26, 2004
Posts: 8
Hi William

You are right about handling "synchronization issues" by using local variables rather than global.

However, the point I tried to make was, a servlet implementing the SingleThreadModel interface, by default gaurantees than the instances are thread safe.

Alternatively explained.. a servlet implementing SingleThreadModel does not explicitly need to handle synchronization issues.

Please correct me if I am wrong.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12809
    
    5
a servlet implementing SingleThreadModel does not explicitly need to handle synchronization issues.

That statement needs to be qualified. There might still be synchronization issues with assets shared with other instances of the servlet. For example, if your servlet used static variables and methods in a helper class, there would be only one instance shared by all servlet instance and access would not be automatically synchronized.
SingleThreadModel does not magically solve all synchronization issues.
Bill
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlets Basic Question