File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet class " Watch "Servlet class " New topic
Author

Servlet class

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
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?


Thanks,
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: May 05, 2008
Posts: 4
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

Joined: Aug 29, 2007
Posts: 212

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 ]

[Servlet tutorial] [Servlet 3.0 Cook Book]
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
what load refers to with respect to servlet?
What happens behind the scene?
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

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

Joined: Oct 30, 2007
Posts: 165
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

Joined: Jul 25, 2006
Posts: 129
Hi Rakesh,

Please refer this FAQ. This will explain your doubt in deepth.


Ashok<br /> <br />SCJP 91%<br />SCWCD 88%
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
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
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
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.


Ping & DNS - my free Android networking tools app
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
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
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
That depends on the server. For Tomcat it's done in the conf/server.xml file; search for "maxThreads".
sudhir nim
Ranch Hand

Joined: Aug 29, 2007
Posts: 212

See

Tomcat docs and this article
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
The service method is always called in its own stack?Means what and How?
 
GeeCON Prague 2014
 
subject: Servlet class