简体   繁体   English

严重:java web 应用程序中的错误 listenerStart

[英]SEVERE: Error listenerStart in java web application

I for the first time trying to use ServletContextListener to execute a perticular function every time application gets deployed.For this i have taken a simple java class file and implemented ServletContextListener on it and declared the listner in web.xml but on deploying it is giving error as我第一次尝试在每次部署应用程序时使用 ServletContextListener 来执行特定的函数。为此,我采用了一个简单的 java 类文件并在其上实现了 ServletContextListener 并在 web.xml 中声明了列表器,但在部署时出现错误作为

SEVERE: Error listenerStart in netbeans ..

Apache tomcat server logs in netbeans..

Nov 15, 2013 11:59:03 AM org.apache.catalina.core.StandardContext listenerStart SEVERE: Error configuring application listener of class app.classes.ContextListenerProcess java.lang.IllegalAccessException: Class org.apache.catalina.core.DefaultInstanceManager can not access a member of class app.classes.ContextListenerProcess with modifiers "" 2013 年 11 月 15 日上午 11:59:03 org.apache.catalina.core.StandardContext listenerStart 严重:配置类 app.classes.ContextListenerProcess java.lang.IllegalAccessException 的应用程序侦听器时出错:类 org.apache.catalina.core.DefaultInstanceManager 可以不使用修饰符“”访问类 app.classes.ContextListenerProcess 的成员

Here is my java class file implementing the ServletContextListener这是我实现 ServletContextListener 的 java 类文件

package app.classes;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;


@WebListener()
class ContextListenerProcess implements ServletContextListener {

@Override
public void contextDestroyed(ServletContextEvent sce) {
}

@Override
public void contextInitialized(ServletContextEvent sce) {
    // Do your startup work here
    System.out.println("Processing Started .....");
}
}

and here is my web.xml adding ContextListenerProcess class ...这是我的 web.xml 添加 ContextListenerProcess 类...

 <listener>
<listener-class>app.classes.ContextListenerProcess</listener-class>
 </listener>

Please guys help me to resolve the issue.. Thanks in advance..请大家帮我解决这个问题.. 提前致谢..

您的ContextListenerProcess类需要是公共的,而不是包私有的。

I have try your code example and it worked for me.我已经尝试过你的代码示例,它对我有用。

    package app.classes;

    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;


    /**
     * Application Lifecycle Listener implementation class ContextListenerProcess
     *
     */
    @WebListener
    public class ContextListenerProcess implements ServletContextListener {

        /**
         * Default constructor. 
         */
        public ContextListenerProcess() {
            // TODO Auto-generated constructor stub
        }

        public void contextDestroyed(ServletContextEvent sce) {
        }

        public void contextInitialized(ServletContextEvent sce) {
            // Do your startup work here
            System.out.println("Processing Started .....");
        }
    }

and this is my web.xml这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Use this definition if using a Java EE 6 container This also stops Eclipse 
    from complaining that 3.0 is not a valid version <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" 
    http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> -->
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <listener>
        <listener-class>app.classes.ContextListenerProcess</listener-class>
    </listener>
    <servlet>
        <description></description>
        <display-name>WebListenerServlet</display-name>
        <servlet-name>WebListenerServlet</servlet-name>
        <servlet-class>app.classes.WebListenerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>WebListenerServlet</servlet-name>
        <url-pattern>/index.html</url-pattern>
    </servlet-mapping>
</web-app>

after i run the application with this configuration it was successful, i see the Processing Started ..... message at the console when tomcat is started.在我使用此配置运行应用程序后,它成功了,当 tomcat 启动时,我在控制台看到了Processing Started .....消息。 I add only我只添加

         <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
        </dependency>

The difference between your code and mine is you put bracket after @WebListener annotation, you should delete it and your ContextListenerProcess class has no access modifier which means it is default, it should be public.你的代码和我的代码之间的区别是你在@WebListener 注释后面加上括号,你应该删除它,你的 ContextListenerProcess 类没有访问修饰符,这意味着它是默认的,它应该是公共的。

I was also getting this error.我也收到了这个错误。 I changed my tomcat to 8.5.24 and it solved my problem.我将我的 tomcat 更改为 8.5.24,它解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM