2013-11-15 32 views
0

我第一次尝试使用的ServletContextListener执行perticular功能,每次应用程序获取deployed.For这个我已经采取了一个简单的Java类文件,并在其上实现了ServletContextListener并宣布读心人在web.xml但上部署它给误差严重:错误listenerStart在Java Web应用程序

SEVERE: Error listenerStart in netbeans .. 

Apache tomcat server logs in netbeans.. 

2013年11月15日上午11时59分03秒org.apache.catalina.core.StandardContext listenerStart 严重:错误配置类的应用监听器app.classes.ContextListenerProcess java.lang.IllegalAccessException:Class org.apache.catalin a.core.DefaultInstanceManager不能访问类app.classes.ContextListenerProcess的成员使用修改器“”

这里是我的Java类文件实现了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 ....."); 
} 
} 

这里是我的web.xml加入ContextListenerProcess类...

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

请你们帮我解决这个问题..提前 谢谢..

+0

检查服务器日志中的详细错误消息 –

+0

@JigarJoshi先生我更新了我的帖子与服务器日志 – Adi

+0

为什么有一个括号旁边的@WebListener – Keerthivasan

回答

0

我试过你的代码示例,它为我工作。

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 ....."); 
     } 
    } 

,这是我的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> 

后,我跑这个配置是成功的应用,我看到在控制台上Processing Started .....消息的Tomcat启动时。我是你的代码之间添加仅

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

的差异和我是你放支架@WebListener注释以后,你应该删除它,你ContextListenerProcess类没有访问修饰符,这意味着它是默认情况下,它应该是公开的。

1

ContextListenerProcess类需要是公共的,而不是包专用。

相关问题