You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by neal <ne...@yahoo.com> on 2002/09/07 09:48:20 UTC

onLoad Servlet

In a previous thread someone mentioned that it is possible to set a servlet
to run as Tomcat is started.

Could someone please provide me with a syntactical example of how to set
this up?

I have searched the documentation, I've searched for exmaples in the web.xml
files, and I've scoured the Internet and I can not find any documentation or
examples.

I guess I'm just looking int he wrong places???

Thanks.
Neal


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: onLoad Servlet

Posted by Ben Walding <be...@walding.com>.
You're probably searching for the wrong thing!
 From web.xml (see http://java.sun.com/dtd/web-app_2_3.dtd)

<!--
The load-on-startup element indicates that this servlet should be
loaded (instantiated and have its init() called) on the startup
of the web application. The optional contents of
these element must be an integer indicating the order in which
the servlet should be loaded. If the value is a negative integer,
or the element is not present, the container is free to load the
servlet whenever it chooses. If the value is a positive integer
or 0, the container must load and initialize the servlet as the
application is deployed. The container must guarantee that
servlets marked with lower integers are loaded before servlets
marked with higher integers. The container may choose the order
of loading of servlets with the same load-on-start-up value.

Used in: servlet
-->
<!ELEMENT load-on-startup (#PCDATA)>

<!--




neal wrote:

>In a previous thread someone mentioned that it is possible to set a servlet
>to run as Tomcat is started.
>
>Could someone please provide me with a syntactical example of how to set
>this up?
>
>I have searched the documentation, I've searched for exmaples in the web.xml
>files, and I've scoured the Internet and I can not find any documentation or
>examples.
>
>I guess I'm just looking int he wrong places???
>
>Thanks.
>Neal
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>  
>




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


RE: onLoad Servlet

Posted by neal <ne...@yahoo.com>.
Micael,

Thank you and that was not offensive at all.  Actually, that is a good
point.  I will look at the Struts web.xml file (presumably in the example
app).  I just didn't know what I was looking for.  :)

Thanks!
neal


-----Original Message-----
From: micael [mailto:caraunltd@harbornet.com]
Sent: Saturday, September 07, 2002 12:47 PM
To: Tomcat Users List
Subject: Re: onLoad Servlet


Hi, neal,

I am going to do my best to state this without being offensive, which I
don't intend to be.  You really need to look at what I am saying, however
you take this, neal.

The problem you are having is why I asked the questions which,
unfortunately, you took as insults.  I thought you were missing this
information and was trying to find out if that was true.  You need to look
at the web.xml for struts, and the dtd for web.xmls generally.  If you look
at the struts example, you will find something like the following in
web.xml:

<web-app>
..........

   <!-- Example Database Initialization Servlet Configuration
   <servlet>
     <servlet-name>database</servlet-name>

<servlet-class>org.apache.struts.example.DatabaseServlet</servlet-class>
     <init-param>
       <param-name>debug</param-name>
       <param-value>2</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
   </servlet>
   -->


   <!-- Standard Action Servlet Configuration (with debugging) -->
   <servlet>
     <servlet-name>action</servlet-name>
     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
     <init-param>
       <param-name>application</param-name>
       <param-value>com.tresbeau.i18n.messages</param-value>
     </init-param>
     <init-param>
       <param-name>config</param-name>
       <param-value>/WEB-INF/xml/struts-config.xml</param-value>
     </init-param>
     <init-param>
       <param-name>debug</param-name>
       <param-value>2</param-value>
     </init-param>
     <init-param>
       <param-name>detail</param-name>
       <param-value>2</param-value>
     </init-param>
     <init-param>
       <param-name>validate</param-name>
       <param-value>true</param-value>
     </init-param>
     <load-on-startup>2</load-on-startup>
   </servlet>


   <!-- Standard Action Servlet Mapping -->
   <servlet-mapping>
     <servlet-name>action</servlet-name>
     <url-pattern>*.do</url-pattern>
   </servlet-mapping>


..........
</web-app>

Additionally, you will find that servlets themselves have an initialization
method (init).  So, you need to delve more into servlets and more into the
way the xml is used to configure things in the Model 2 architecture.

The core of the struts app, of course, is the ActionServlet.  With that in
mind, consider the following class:

package com.oreilly.struts.storefront.framework;

import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import org.apache.struts.action.ActionServlet;
import com.oreilly.struts.storefront.service.IStorefrontService;
import com.oreilly.struts.storefront.service.StorefrontServiceImpl;
import com.oreilly.struts.storefront.framework.util.IConstants;
import
com.oreilly.struts.storefront.framework.exceptions.DatastoreException;
/**
  * Extend the Struts ActionServlet to perform your own special
  * initialization.
  */
public class ExtendedActionServlet extends ActionServlet {

   public void init() throws ServletException {

     // Make sure to always call the super's init() first
     super.init();

     // Initialize the persistence service
     try{
       // Create an instance of the service interface
       StorefrontServiceImpl serviceImpl = new StorefrontServiceImpl();

       // Store the service into the application scope
       getServletContext().setAttribute( IConstants.SERVICE_INTERFACE_KEY,
                                         serviceImpl );
     }catch( DatastoreException ex ){
       // If there's a problem initializing the service, disable the web app
       ex.printStackTrace();
       throw new UnavailableException( ex.getMessage() );
     }
   }
}
This is from Chuck's upcoming (soon) book.  This ought to be enough to get
you kick started.  I once again highly recommend that you read Jason
Hunter's book on servlets.

micael


At 12:48 AM 9/7/2002 -0700, you wrote:
>In a previous thread someone mentioned that it is possible to set a servlet
>to run as Tomcat is started.
>
>Could someone please provide me with a syntactical example of how to set
>this up?
>
>I have searched the documentation, I've searched for exmaples in the
web.xml
>files, and I've scoured the Internet and I can not find any documentation
or
>examples.
>
>I guess I'm just looking int he wrong places???
>
>Thanks.
>Neal
>
>
>--
>To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
>For additional commands, e-mail:
<ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: onLoad Servlet

Posted by micael <ca...@harbornet.com>.
Hi, neal,

I am going to do my best to state this without being offensive, which I 
don't intend to be.  You really need to look at what I am saying, however 
you take this, neal.

The problem you are having is why I asked the questions which, 
unfortunately, you took as insults.  I thought you were missing this 
information and was trying to find out if that was true.  You need to look 
at the web.xml for struts, and the dtd for web.xmls generally.  If you look 
at the struts example, you will find something like the following in web.xml:

<web-app>
..........

   <!-- Example Database Initialization Servlet Configuration
   <servlet>
     <servlet-name>database</servlet-name>
     <servlet-class>org.apache.struts.example.DatabaseServlet</servlet-class>
     <init-param>
       <param-name>debug</param-name>
       <param-value>2</param-value>
     </init-param>
     <load-on-startup>1</load-on-startup>
   </servlet>
   -->


   <!-- Standard Action Servlet Configuration (with debugging) -->
   <servlet>
     <servlet-name>action</servlet-name>
     <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
     <init-param>
       <param-name>application</param-name>
       <param-value>com.tresbeau.i18n.messages</param-value>
     </init-param>
     <init-param>
       <param-name>config</param-name>
       <param-value>/WEB-INF/xml/struts-config.xml</param-value>
     </init-param>
     <init-param>
       <param-name>debug</param-name>
       <param-value>2</param-value>
     </init-param>
     <init-param>
       <param-name>detail</param-name>
       <param-value>2</param-value>
     </init-param>
     <init-param>
       <param-name>validate</param-name>
       <param-value>true</param-value>
     </init-param>
     <load-on-startup>2</load-on-startup>
   </servlet>


   <!-- Standard Action Servlet Mapping -->
   <servlet-mapping>
     <servlet-name>action</servlet-name>
     <url-pattern>*.do</url-pattern>
   </servlet-mapping>


..........
</web-app>

Additionally, you will find that servlets themselves have an initialization 
method (init).  So, you need to delve more into servlets and more into the 
way the xml is used to configure things in the Model 2 architecture.

The core of the struts app, of course, is the ActionServlet.  With that in 
mind, consider the following class:

package com.oreilly.struts.storefront.framework;

import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import org.apache.struts.action.ActionServlet;
import com.oreilly.struts.storefront.service.IStorefrontService;
import com.oreilly.struts.storefront.service.StorefrontServiceImpl;
import com.oreilly.struts.storefront.framework.util.IConstants;
import com.oreilly.struts.storefront.framework.exceptions.DatastoreException;
/**
  * Extend the Struts ActionServlet to perform your own special
  * initialization.
  */
public class ExtendedActionServlet extends ActionServlet {

   public void init() throws ServletException {

     // Make sure to always call the super's init() first
     super.init();

     // Initialize the persistence service
     try{
       // Create an instance of the service interface
       StorefrontServiceImpl serviceImpl = new StorefrontServiceImpl();

       // Store the service into the application scope
       getServletContext().setAttribute( IConstants.SERVICE_INTERFACE_KEY,
                                         serviceImpl );
     }catch( DatastoreException ex ){
       // If there's a problem initializing the service, disable the web app
       ex.printStackTrace();
       throw new UnavailableException( ex.getMessage() );
     }
   }
}
This is from Chuck's upcoming (soon) book.  This ought to be enough to get 
you kick started.  I once again highly recommend that you read Jason 
Hunter's book on servlets.

micael


At 12:48 AM 9/7/2002 -0700, you wrote:
>In a previous thread someone mentioned that it is possible to set a servlet
>to run as Tomcat is started.
>
>Could someone please provide me with a syntactical example of how to set
>this up?
>
>I have searched the documentation, I've searched for exmaples in the web.xml
>files, and I've scoured the Internet and I can not find any documentation or
>examples.
>
>I guess I'm just looking int he wrong places???
>
>Thanks.
>Neal
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>