• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Servlet works?

 
ramakrishna rayudu
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,,

i have big doubt in my Mind.How Servlet Works??
i have seen so many artical "when the request comes init() method is called and servlet is processed.when the an other request came for the same Servlet
Servlet Container will checkes for The Instance of The Servlet is Avaliable are not if there it will process the process the Request other wise it Create
The Instance of that Servlet and Process
".

and here MyQuestion how Servlet will be Processed when Two Requests came for same servlet at the same time will container create two insatnces-objects for same Servlets???
is Servlet is Single Turn Class??

Thanks,
Ramakrishna Rayudu
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a single instance of the servlet. A thread is created by the container to service each thread.

This is why it is important to write servlets in a thread-safe manner.
 
ramakrishna rayudu
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i Write The Servlet in Thread Safe Manner???
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difficult question which is more about threading than servlets.
Thread safety is about data sharing and data protection.
Essentially you need to be aware of which data is visible to multiple threads and make sure that you don't alter data while it is being read, or that other threads can see an object in an incomplete state while it is being created.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David is absolutely correct. But a great deal of the work is accomplished by simply avoiding instance variables. There are tons of other things to worry about as David pointed out, but instances variables are usually what get people into trouble.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
absolutely. Instance variables have no place in a servlet*

(* as with many 'absolutes' there are caveats but not worth addressing at this stage. Just say 'No')
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic