class没有在服务器启动时初始化

时间:2013-06-25 20:08:50

标签: tomcat web.xml

我想在服务器启动时调用以下类。

AlertsData位于com.mongo.service:

public  class AlertsData extends HttpServlet{
    public void init(ServletConfig config) throws ServletException{
        super.init(config);
        alertingPoolsData();
    }
}    

的web.xml:

<servlet-mapping>
    <servlet-name>AlertsData</servlet-name>
    <display-name>AlertsData</display-name>
    <servlet-class>com.mongo.service.AlertsData</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet-mapping>

得到以下错误:

SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/lbmongo]]
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:222)
    at java.util.concurrent.FutureTask.get(FutureTask.java:83)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1123)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:785)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

这台服务器没有启动并且出现以下错误..一些请帮助:(    我想在服务器启动时调用AlertsData类。

1 个答案:

答案 0 :(得分:0)

web.xml中的servlet配置看起来无效。 servlet-classload-on-startup等元素属于servlet部分,而不属于servlet-mapping部分。您应该同时拥有servlet部分和servlet-mapping部分,例如:

<servlet>
    <servlet-name>AlertsData</servlet-name>
    <display-name>AlertsData</display-name>
    <servlet-class>com.mongo.service.AlertsData</servlet-class>
    <load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>AlertsData</servlet-name>
    <url-pattern>/alerts</url-pattern>
</servlet-mapping>

url-pattern下的sevlet-mapping是您要用来访问servlet的网址(用您自己的网址替换“/alerts”。)