• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlets Basic Question

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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






 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic