You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "HODAC, Olivier" <OL...@airbus.com> on 2007/10/12 14:26:20 UTC

newby user: processing when the server launches

Hello,

 
sorry for this newby question.
 
I am developping my first webapp, and I need to do things at the server launching step.
 
I understood that when a client sends the first request to the server, it loads the required classes and instanciates them. So, I have to wait for the first client to connect.
 
In my application, I have to perform stuff when the server starts. Where do I have to put this code?
 
I have red that it is possible to force the class loading at the webapp launch. is this the solution?
 
ex
 
public class myClass {
{

System.out.println("loaded myClass");

//my stuff

}

 
}
 
thanks
 


This e-mail is intended only for the above addressee. It may contain privileged information.
If you are not the addressee you must not copy, distribute, disclose or use any of the information in it. 
If you have received it in error please delete it and immediately notify the sender.
Security Notice: all e-mail, sent to or from this address, may be accessed by someone other than the recipient, for system management and security reasons. This access is controlled under Regulation of security reasons.
This access is controlled under Regulation of Investigatory Powers Act 2000, Lawful Business Practises.



RE: newby user: processing when the server launches

Posted by Peter Crowther <Pe...@melandra.com>.
> From: HODAC, Olivier [mailto:OLIVIER.HODAC@airbus.com] 
> In my application, I have to perform stuff when the server 
> starts. Where do I have to put this code?

Define and use a servlet context listener.  This will run as your webapp
starts up.

http://www.onjava.com/pub/a/onjava/2001/04/12/listeners.html

		- Peter

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: newby user: processing when the server launches

Posted by Juha Laiho <Ju...@iki.fi>.
On Fri, 12 Oct 2007, HODAC, Olivier wrote:
> I am developping my first webapp, and I need to do things at the
> server launching step.
>
> I understood that when a client sends the first request to the server,
> it loads the required classes and instanciates them. So, I have to
> wait for the first client to connect.

Yes, pretty much everything in Java is loaded on demand.

> In my application, I have to perform stuff when the server starts.
> Where do I have to put this code?

There are two ways:
In old systems (where the container implements Java Servlet specification
version 2.2 or earlier) you declare a servlet to be initialized on startup.
You then call your application initialization routines from the init()
method of this servlet. To keep things organised, it might be best that
the servlet you use for application initialization does not participate
in handling user events.

In recent systems (so, for Servler spec v2.3 and later), you write
a ServletContextListener to call your startup code.

If your application contains some objects that are designed to be
available for the whole lifetime of the web application, you place
those objects available in the ServletContext.

For the details, please see the Java Servlet Specification
(pick a version that matches what is implemented by your application
server).
-- 
Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
         PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org