开发者

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

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>


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:

  • Servletcontainer lifecycle


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.


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)
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜