load-on-startup Tomcat

前提是你 提交于 2019-12-18 11:48:05

问题


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

EDIT: How can I implements this class and xml?

<servlet-name>??</servlet-name>
<servlet-class>??</servlet-class>
<load-on-startup>10</load-on-startup>

回答1:


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



回答2:


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.




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



回答4:


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.



来源:https://stackoverflow.com/questions/3289737/load-on-startup-tomcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!