• 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

Some Clarification about servlet ??

 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A simple instance variable initialized to 0 should work for you.
You do not need a static qualifier here.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !!!
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
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

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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike,
Thanks for ur reply.Now it is clear to me.
Thanks & Regards
Bikash
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic