aspose file tools*
The moose likes Servlets and the fly likes How Servlet Container is Working? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How Servlet Container is Working?" Watch "How Servlet Container is Working?" New topic
Author

How Servlet Container is Working?

Pandian Raman
Greenhorn

Joined: Jun 21, 2011
Posts: 12
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

Joined: Jan 10, 2002
Posts: 61756
    
  67

There are nuances to that, but yeah, pretty close.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
vinayak jog
Ranch Hand

Joined: Apr 01, 2011
Posts: 81

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

Joined: Jun 30, 2012
Posts: 13
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.


Anirudh
PMP, OCJCP 6
Pandian Raman
Greenhorn

Joined: Jun 21, 2011
Posts: 12
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
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

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

Joined: Jun 30, 2012
Posts: 13
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

Joined: Jun 21, 2011
Posts: 12
Thanks Anirudh, I will gothrough it and i will come back with clarification.
Mukesh Negi
Greenhorn

Joined: Jul 25, 2012
Posts: 17
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

Joined: Jan 10, 2002
Posts: 61756
    
  67

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How Servlet Container is Working?