Wouldn't it be better to develop a ServletContextListener servlet?
Anyhow,
I did give it a try and I attempt to develop a ServletContextListener
servlet.
This is the code I'm using (took it from tomcat servlet samples):
/**
*
*/
package starters;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextServletsTest implements ServletContextAttributeListener,
ServletContextListener {
/**
* The servlet context with which we are associated.
*/
private ServletContext context = null;
// --------------------------------------------------------- Public
Methods
/**
* Record the fact that a servlet context attribute was added.
*
* @param event The servlet context attribute event
*/
public void attributeAdded(ServletContextAttributeEvent event) {
log("attributeAdded('" + event.getName() + "', '" +
event.getValue() + "')");
}
/**
* Record the fact that a servlet context attribute was removed.
*
* @param event The servlet context attribute event
*/
public void attributeRemoved(ServletContextAttributeEvent event) {
log("attributeRemoved('" + event.getName() + "', '" +
event.getValue() + "')");
}
/**
* Record the fact that a servlet context attribute was replaced.
*
* @param event The servlet context attribute event
*/
public void attributeReplaced(ServletContextAttributeEvent event) {
log("attributeReplaced('" + event.getName() + "', '" +
event.getValue() + "')");
}
/**
* Record the fact that this web application has been destroyed.
*
* @param event The servlet context event
*/
public void contextDestroyed(ServletContextEvent event) {
log("contextDestroyed()");
this.context = null;
}
/**
* Record the fact that this web application has been initialized.
*
* @param event The servlet context event
*/
public void contextInitialized(ServletContextEvent event) {
this.context = event.getServletContext();
log("contextInitialized()");
}
// -------------------------------------------------------- Private
Methods
/**
* Log a message to the servlet context application log.
*
* @param message Message to be logged
*/
private void log(String message) {
if (context != null)
context.log("ContextListener: " + message);
else
System.out.println("ContextListener: " + message);
}
/**
* Log a message and associated exception to the servlet context
* application log.
*
* @param message Message to be logged
* @param throwable Exception to be logged
*/
private void log(String message, Throwable throwable) {
if (context != null)
context.log("ContextListener: " + message, throwable);
else {
System.out.println("ContextListener: " + message);
throwable.printStackTrace(System.out);
}
}
}
I also added a Listener entry in the application web.xml:
<listener>
<listener-class>starters.ContextServletsTest</listener-class>
</listener>
For some reason its not working and I can't figure it out.
Apperently I did every thing as required. Yet it doesn't function at all.
Thanks in advance,
Asaf Lahav
VP R&D, Prima Grid LTD.
Cellular: 972-54-4717955
Phone: 972-3-6540255
Fax: 972-3-6540254
-----Original Message-----
From: Parsons Technical Services [mailto:[EMAIL PROTECTED]
Sent: Monday, May 15, 2006 11:45 PM
To: Tomcat Users List
Subject: Re: Run class in the start up of Tomcat
Are you wanting to start a servlet or load a class?
You can load a servlet at the application level.
load-on-startup in the servlet class element in the web.xml of the app.
<servlet>
<servlet-name>Scored</servlet-name>
<servlet-class>srm.Scored</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
The number indicates the order that the servlet is loaded.
As for a class you will need to look into the class loader for Tomcat.
http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html
Doug
----- Original Message -----
From: "Juanjo Cuadrado" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Monday, May 15, 2006 5:08 PM
Subject: Run class in the start up of Tomcat
Hi,
I'm trying to run a class in startup of Tomcat. I think that this was
possible in others versions of Tomcat (I just started with Tomcat 5). I
think that it was a property in someone element of server.xml that allowed
this.
Anyone can help me? I hope that yes ;) tx
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]