Can load-on-startup in web.xml be used to load an arbitrary class on startup?
Asked Answered
D

4

17

How can I load an arbitrary class on startup in Tomcat? I saw load-on-startup tag for web.xml file, but can I use it and how should I implement my class?

<servlet-name>??</servlet-name>
<servlet-class>??</servlet-class>
<load-on-startup>10</load-on-startup>
Durr answered 20/7, 2010 at 12:11 Comment(0)
P
31

Those are meant to specify the loading order for servlets. However, servlets are more meant to control, preprocess and/or postprocess HTTP requests/responses while you sound like to be more looking for a hook on webapp's startup. In that case, you rather want a ServletContextListener.

@WebListener
public class Config implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {
        // Do your thing during webapp's startup.
    }
    public void contextDestroyed(ServletContextEvent event) {
        // Do your thing during webapp's shutdown.
    }
}

If you're not on Servlet 3.0 yet (and thus can't use @WebListener), then you need to manually register it in web.xml as follows:

<listener>
    <listener-class>com.example.Config</listener-class>
</listener>

See also:

Pettifer answered 20/7, 2010 at 12:16 Comment(1)
If I want to startup my application with an specific servlet (which is not possible to be a static page) is this here the only way? I would use your ContextListener with an forward?Asseverate
I
8

The element load-on-startup indicates that this servlet should be loaded (instantiated and have its init() called) on the startup of the Web application. The element content of this element must be an integer indicating the order in which the servlet should be loaded.In other words, container loads the servlets in ascending integer value. The 0 value will be loaded first then 1, 2, 3 and so on.

Let's try to understand it by the example given below:

web.xml

<web-app>  
 ....  
  //=====================servlet 1==============
  <servlet>  
   <servlet-name>servlet1</servlet-name>  
   <servlet-class>com.javatpoint.FirstServlet</servlet-class>  
   <load-on-startup>0</load-on-startup>  //value given 0(zero)
  </servlet>  

  //=====================servlet 2==============
  <servlet>  
   <servlet-name>servlet2</servlet-name>  
   <servlet-class>com.javatpoint.SecondServlet</servlet-class>  
   <load-on-startup>1</load-on-startup>   //value given 1(one)  
  </servlet>  

 ...  
</web-app>  

There are defined 2 servlets, both servlets will be loaded at the time of project deployment or server start. But, servlet1 will be loaded first then servlet2.

Passing negative value : If you pass the negative value, servlet will be loaded at request time, at first request.

Inefficacy answered 25/4, 2016 at 9:13 Comment(0)
W
3

enfix,

Your XML looks good.

You should place a init() method in your servlet class, that gets called when your server bootsup. doGet, doPost and do methods get called only when there is an incoming request.

public class YourServlet extends HttpServlet
{
    public void init()
    {
        //initialize( or add a log statement to debug)
    }
}
Walrath answered 29/7, 2013 at 6:38 Comment(1)
This is the best answer here - because with the load-on-startup integer you can control that eg. your servlet is being started after all other load-on-startup servlets. If you don't care, then a ServletContextListener should be uses, which is run before all these startup servlets.Indigo
M
1

This is the solution for Tomcat 7.0 Step 1: Create war file for your webapp/servlets. If you are using Eclipse, File->Export->Web->WAR file, and save it to a known location.

Step 2: Find out the home folder for your tomcat. For that, go to tomcat/apache-tomcat-7.0.41/bin and execute ./startup.sh This will print out couple of global variable names. Note down the one for CATALINA_HOME.

Step 3: Copy the war file from Step 1 in CATALINA_HOME/webapps

Step 4: Next, Create an xml file in CATALINA_HOME/conf/{Engine}/localhost/MyServlets.xml :

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<Context deployOnStartup="true" docBase="/home/ubuntu/Downloads/apache-tomcat-7.0.42/webapps/" reloadable="true">
<Manager pathname=""/>
</Context>

Change docBase to point to location where you copied the war file in Step 3.

Now, you can go go to tomcat/apache-tomcat-7.0.41/bin and execute ./startup.sh. Your servlets will be automatically started. Hope this helps.

Mahayana answered 29/7, 2013 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.