"Gopalankutty, Ravi Kumar (CTS)" wrote:

>
>
> Hi list,
>
> I am slightly confused with the servlet life cycle. Say, I invoke a
> servlet (LoginServlet) from a html page. This invoked servlet goes
> through an init phase,handles the service call in which it throws
> another page dynamically. When the user gets this display on his
> browser, is that when the servlet destroys itself?
>

That is not what actually happens.

The first time *any* request for a particular servlet is received, the
init() method is called.  (This could also happen when the servlet
engine is started, if you configure things that way).  Then, every
request goes immediately to the service() method.  Finally, when the
servlet engine is shutting down, or otherwise it decides it wants to,
the servlet's destroy() method is called.

>
> Supposing another client requests for the same servlet then, does the
> server create another instance in which case the init cycle is not
> performed ( am I right here???) or does it create another servlet
> object to service the second call?
>

As described above, the cycle is not performed.  If the servlet has
already been initialized, it will be reused.

>
> The reason for this question is because I want to maintain the
> connections (to databases) that I make in the init method of the first
> servlet to service all requests to that servlet.
>

You can do that.  If your application only has a single servlet you can
store the database connection in an instance variable of the servlet
itself.  If you've got more than one, the simplest thing to do would be
store it as a servlet context attribute.

One more thing to keep in mind, however -- if two requests come in
simultaneously, they will *both* be executing at the same time and thus
using the same database connection.  This often causes problems, so most
people use a "connection pool" rather than a single "connection" for
this purpose.  There has been lots of discussion on this mailing list
about connection pools; see the archives for details.

>
> Does Apache support such instance persistance mechanism?
>

Yes (when you use a servlet engine like Apache JServ or Tomcat with it),
as does every other servlet engine.

>
> Thanks k Regards
>
> Ravi Kumar Gopalankutty

Craig McClanahan

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to