加载启动Tomcat

时间:2010-07-20 12:11:05

标签: tomcat servlets web.xml

如何在Tomcat中启动时加载类? 我看到load-on-startup文件的web.xml标记,但是我可以使用它吗?我该如何编写课程?

编辑:我如何实现这个类和xml?

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

4 个答案:

答案 0 :(得分:30)

这些用于指定servlet的加载顺序。但是,servlet更倾向于控制,预处理和/或后处理HTTP请求/响应,而您听起来更像是在webapp的启动时寻找钩子。在这种情况下,您更需要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.
    }
}

如果您尚未使用Servlet 3.0(因此无法使用@WebListener),则需要在web.xml中手动注册,如下所示:

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

另见:

答案 1 :(得分:4)

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.

答案 2 :(得分:2)

enfix,

您的XML看起来不错。

您应该在servlet类中放置一个init()方法,该方法在服务器启动时被调用。只有在有传入请求时才会调用doGet,doPost和do方法。

public class YourServlet extends HttpServlet
{
    public void init()
    {
        //initialize( or add a log statement to debug)
    }
}

答案 3 :(得分:1)

这是Tomcat 7.0的解决方案 步骤1:   为您的webapp / servlets创建war文件。   如果您使用的是Eclipse,File-&gt; Export-&gt; Web-&gt; WAR文件,请将其保存到已知位置。

第2步:   找出tomcat的主文件夹。   为此,请转到tomcat / apache-tomcat-7.0.41 / bin并执行./startup.sh   这将打印出几个全局变量名称。   记下CATALINA_HOME的那个。

第3步:   从CATALINA_HOME / webapps

中的步骤1复制war文件

第4步:   接下来,在CATALINA_HOME / conf / {Engine} /localhost/MyServlets.xml中创建一个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>

将docBase更改为指向您在步骤3中复制war文件的位置。

现在,您可以转到tomcat / apache-tomcat-7.0.41 / bin并执行./startup.sh。 您的servlet将自动启动。 希望这会有所帮助。

相关问题