문제

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

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 ""

Here is my java class file implementing the ServletContextListener

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 ...

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

Please guys help me to resolve the issue.. Thanks in advance..

도움이 되었습니까?

해결책 2

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

<?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. 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.

다른 팁

Your ContextListenerProcess class needs to be public rather than package private.

I was also getting this error. I changed my tomcat to 8.5.24 and it solved my problem.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top