Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet class

 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to servlets.I have some doubts due to which i am not able to proceed.

I have Tomcat 5 deployed on my system. I want to know:

1. When is Servlet class loaded?How?What actually happens?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless set as 'load on startup', a servlet will be loaded when it gets requested.

What actually happens?
The servlet gets loaded. What information are you looking for?
 
Shashi Achanta
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean that you wish to know what happens in servlet life cycle?

When the application is accessed using a URL, the URL will be pointing to a particular servlet.That particular servlet will then be loaded and life cycle occurs. The mapping between the URL and the servlet is available in deployment descriptor of the application, in our case web.xml.

The init() of the servlet is called and then the service() is called. The service() then calls the doGet() or doPost() accordingly, in case of a HttpServlet.

I would seriously suggest you read the Chapter 4 of Head First Servlets & JSPs.
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Usually servlet is loaded when the request is received for a servlet which is not loaded yet. you can make the container load the servlets at the server startup by specifying load-on-startup parameter in web.xml

1) Container loads all the servlet with load-on-startup param when server starts

2. When ever any request is received, based on URL patters, container determines if the request is to be handled by a servlet.

3. If request is to be handled by a servlet, container checks if servlet is already loaded and initialized, if so handover the request to it.

3. If servlet is not loaded,container creates a new instance of the servlet, initializes it by calling its init() method. Once servlet is initialize, handover the request to that servlet.

Fore more info, google for 'servlet lifecycle'
[ May 05, 2008: Message edited by: sudhir nim ]
 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what load refers to with respect to servlet?
What happens behind the scene?
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Load means instantiations of the servlet class.
Web container creates a new instance (object) of the servlet class using the newInstance() method.
 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we do not override the init(),default init() from GenericServlet runs.

What the init() from GenericServlet executes? or what does this init() initializes the servlet with?
 
Ashok Kumar Babu
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rakesh,

Please refer this FAQ. This will explain your doubt in deepth.
 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went through the FAQ's,but I was not able to understand the ans for below 2 questions.

Why do servlets have an init method? Can't we make use of the servlet constructor for initialization?
Why can't a container call constructor having parameters?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor can't have parameters because then the servlet container would not know how to instantiate it - it wouldn't know what to pass as parameters.
 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each request made by a client will be a thread containing request and response objects to the servlet instance.

At max what number of threads for client request are present.
If we configure,Where can it be done?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on the server. For Tomcat it's done in the conf/server.xml file; search for "maxThreads".
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The service method is always called in its own stack?Means what and How?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic