Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Servlet Container is Working?

 
Pandian Raman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per analysis this is how Servlet is working.

When a request is comes to the server from browser, then the server will be delegate the request to the servlet container.
Then the servlet container will identify the respective request’s servlet class based on the configuration xml.
If the servlet is called for the first time by the servlet container, then the servlet container will load the servlet class and call its init method only once.
Then for each and every request, the servlet container will construct an instance of javax.servlet.ServletRequest and an instance of javax.servlet.ServletResponse.
Then the servlet container will invoke the servlet’s service method by passing the ServletRequest and ServletResponse objects.
When the server is shutdown or the servlet class is shutdown then the servlet container will invoke the servlet’s destroy method and unload the servlet class.

Guys, Please share your knowledge.

Thanks in Advance .
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are nuances to that, but yeah, pretty close.
 
vinayak jog
Ranch Hand
Posts: 83
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:There are nuances to that, but yeah, pretty close.

What are those nuances ? any links for digging it deeper...?
 
Anirudh Srivastav
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinayak

Hope this http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading helps you!

Would like to know experts view from Bear as well.

 
Pandian Raman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anirudh,

The link which you provided is really help me lot to understand the functionalities of ServletContainer.
But i was bit confused now
1) How the servlets are initilized which are configured in web.xml without a request from a client?
2) Without a request if a servlet is get initilized then how servlet container will create and assign JSESSIONID and SESSIONID in cookie?
3) How a single instance of servlet will serve multiple client request without sharing the data?
4) Suppose I have instantiated two servlet, but i am using only one servet for more than 30 mins( which is session timeout duration), then what about the status of another servlet, whether still alive or dead? In otherwords whether the session timeout will work for each and every servlet individually or mutually?

Hi All,

Like "Anirudh", even i am also expecting more details from the experts like Bear and all.
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pandian Raman wrote:1) How the servlets are initilized which are configured in web.xml without a request from a client?


The servlet container calls the init() method of the servlet as part of the startup process.

2) Without a request if a servlet is get initilized then how servlet container will create and assign JSESSIONID and SESSIONID in cookie?


What cookie? And what does that have to do with servlet initialization? The cookies are only used to support sessions.

3) How a single instance of servlet will serve multiple client request without sharing the data?


It's a Java object so it can be used from more than one thread at the same time. Just like any other Java object, its instance variables are accessible to all threads which are using it but its local variables are specific to the thread which called the method.

4) Suppose I have instantiated two servlet, but i am using only one servet for more than 30 mins( which is session timeout duration), then what about the status of another servlet, whether still alive or dead? In otherwords whether the session timeout will work for each and every servlet individually or mutually?


Again you are confusing servlets with sessions. Sessions can time out. Servlets do not. They do not have a "status". They cannot be "dead".

 
Anirudh Srivastav
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pandian,

Paul has cleared your queries, apologies for the delay in reply. Was busy with my WCD certification preparation.

I hope you are learning Servlets and JSP (just like me ) and hence would suggest you to read HF Servlets and JSP (first six chapters).
It s pretty easy and straight forward and might take around 30 hours at the max.

If you are not able get HF Servlets and JSP, then try Core Servlets and JavaServer Pages by Marty Hall. First 9 chapters. Entire book is available as a free download.

http://volume2.coreservlets.com/

I felt most of queries would not have come in case you would read the chapters mentioned.

Good luck to you.

 
Pandian Raman
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anirudh, I will gothrough it and i will come back with clarification.
 
Mukesh Negi
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anirudh Srivastav wrote:Vinayak

Hope this http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading helps you!

Would like to know experts view from Bear as well.




Hi @Anirudh, in the above link I found
The webapp's web.xml will be parsed and every Servlet, Filter and Listener found in web.xml will be created once and kept in server's memory as well. When the servletcontainer shuts down, it will unload all webapplications and the ServletContext and all Servlet, Filter and Listener instances will be trashed


But I have read that Servlets are loaded and Intsantiated when the request for that specific Servlet comes.

Please correct me if I am wrong.

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly when the servlet is instantiated depends upon a number of factors. At the very latest, it will be created when a request comes in for it.

For exact details, see the Servlet Specification.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic