Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basics of servlets

 
Sangram Singh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to servlets and have just started reading HFSJ. From first few chapters (3)what I have understood about servlets is that when the conatiner starts it loads the all the selvlets and after that for each request conatiner creates a new thread and calls the service method of the "same servlet instance" in each thread.
so why the service and the doGet and doPost methods are not synchronized???

Am I missing something ??


Thanks and Regards
Sangram Singh
 
Lalit mishra
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that for each request a new thread is created or invoke
and concurrent request doesnt have any relation .
It has its own HttpServletRequest and HttpServletResponse .

Suppose i make doGet or doPost as synchronized then only one thread can only access these method and all other request will have to wait .

In a web-application there can be many simultaneous request so making this method synchronized would be a performance hit.
 
Sangram Singh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lalit,

Thanks for a quick reply...but I think It should be very common to create a instance(shared) variable in your servlet class (again I am VERY new to servlets).is the overhead of creating a new servlet instance is really tooooooooooooooo much.


Thanks and Regards
Sangram Singh
[ August 17, 2006: Message edited by: Sangram Singh ]
 
Mohd Fuzail
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to make things more clear....

We have one servlet instance, as soon as a request comes in for that servlet and new thread is created and servlet service method is called.

Synchronizing service/doGet/doPost methos implies that only one thread at a time can execute those methods
 
akshay pandit
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
synchronizing service method would be like saying "Hey instead of syncronizing service method why not you implement SingleThreadModel (by the way I'm depricated) " and if you use , either of the two approaches.. then it would be sucidal attempt for your web-application, as the servlet of your web-app will only be handling one user at a time.
 
Sangram Singh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok..so for handling multiple request in multithreaded way, we don't synchronize service /doPost/doget methods.BUT most of the time there will b multiple request coming for a particular servlet at a time and if we are using shared variable then we(programmer) will have to take care of the synchronization part.Well this overhead of synchronization will not be there if for each request container is creating a new servlet, I think its(Container) not made to work like this because of overhead of servlet creation.

Can anybody tell me what exactly is this overhead?


or there is some other reason?


Is there any specification from Sun which says container should not create instances of servlet on each request? or this is implementation(of container) dependent?

I am sorry for too many questions... and thanks for your patience
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why servlets are reused: because it's darn expensive to keep creating new ones and throwing them out.

Just because multiple threads can concurrently hit a method doesn't mean we have to synchronize it. Think about the servlet methods. What danger is there in two threads running those methods concurrently?
 
Sangram Singh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your reply Marc...

I have heard that websphere keeps pool of servlet instances. so I will settle down with following..


ONLY for performance reason single servlet instance is used AND its not the part of the specification.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic