• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Multiple Requests for a Servlet class are handled

 
Raghavendra Nittur
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Am new to servlets. I have doubt.

Since for each servlet, the container loads a single instance of the class and calls init method written for it.

In web, at a time multiple users will be accessing our pages. How Container handles Multiple Requests to a servlet class?


Regards, Raghav
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The container would make threads for the servlets. Well you also have a single thread model (now deprecated) which wouldn't make multiple instances. I feel that a tutorial (google search) would be a better help for the same.
 
Ceenu Reddy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the servlet implements the SingleThreadModel interface then no two threads will execute the service() method concurrently. If there are more than one request for the same servlet then the container will responsible to create the pool of servlet instances and dispatch each new request to the free one.

Please refer the Servlet API for more information.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
better to pretend that the SingleThreadModel doesn't exist. It is a hack that doesn't actually provide complete thread safety so it is better to learn to protect data.
 
Raghavendra Nittur
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ceenu Reddy:
If there are more than one request for the same servlet then the container will responsible to create the pool of servlet instances and dispatch each new request to the free one.



There will be a single instance of each servlet class. If am not Wrong.

Container will responsible means.... I need not to make any code change to handle multiple requests. ( other than session management) ?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. The container would actually do that for you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavendra Nittur:
means.... I need not to make any code change to handle multiple requests. ( other than session management) ?


You must, of course, write your servlets in a thread-safe manner.
 
Raghavendra Nittur
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

write your servlets in a thread-safe manner.


Can you suggest me how i can write my servlets in a thread safe manner.

What do i need to refer. Am new to Servlets.

Reards, Raghav
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghavendra Nittur:


Can you suggest me how i can write my servlets in a thread safe manner.

What do i need to refer. Am new to Servlets.

Reards, Raghav


Since now you have understood that multiple threads (one for each client's request) will execute service() method at the same time, you need to make sure that your data is in consistent state.

There are different scope objects exist in JSP/Servlet (I hope the word is right): request, session, config, context and page (page is only in JSP).

Request data are by default thread safe since one thread per request.
Session data, you don�t need to make thread safe because you will intentionally want to update it.
Config and Context, you need to take care of...

I am 100% sure I am not 100% correct above. Please someone correct me.
Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!