0

已觀察到,每當我添加一個Listener類我web.xml,整個Web應用程序停止運行。請看以下代碼:的ServletContextListener問題| HTTP狀態404 -

的web.xml

<listener> 
    <listener-class>Reminder</listener-class> 
</listener> 

Reminder.java

@Override 
public void contextInitialized(ServletContextEvent sce) { 
    try { 
     response.getWriter().print("Initasdadsa"); 
    } catch (IOException ex) { 
     Logger.getLogger(Reminder.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

@Override 
public void contextDestroyed(ServletContextEvent sce) { 
    Enumeration<Driver> drivers = DriverManager.getDrivers(); 
    while (drivers.hasMoreElements()) { 
     Driver driver = drivers.nextElement(); 
     try { 
      DriverManager.deregisterDriver(driver); 
     } catch (SQLException e) { 
     } 

    } 
} 

當我部署上述應用程序的.war文件,它說:

HTTP Status 404 - type Status report 
message description The requested resource is not available.Apache Tomcat/6.0.24 

Tomcat的日誌文件如下:

May 9, 2013 3:57:10 PM org.apache.catalina.startup.HostConfig checkResources 
INFO: Undeploying context [/CloudStorage] 
May 9, 2013 3:57:10 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc 
SEVERE: A web application registered the JBDC driver [com.mysql.jdbc.Driver] but failed   
to unregister it when the web application was stopped. To prevent a memory leak, the 
JDBC Driver has been forcibly unregistered. 
May 9, 2013 3:57:50 PM org.apache.catalina.startup.HostConfig deployWAR 
INFO: Deploying web application archive CloudStorage.war 
May 9, 2013 3:57:50 PM org.apache.catalina.core.StandardContext start 
SEVERE: Error listenerStart 
May 9, 2013 3:57:50 PM org.apache.catalina.core.StandardContext start 
SEVERE: Context [/CloudStorage] startup failed due to previous errors 
May 9, 2013 3:57:50 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc 
SEVERE: A web application registered the JBDC driver [com.mysql.jdbc.Driver] but failed  
to unregister it when the web application was stopped. To prevent a memory leak, the 
JDBC Driver has been forcibly unregistered. 

當我省略了Listener一部分,該應用程序工作正常。 :-(

我在做什麼錯?

+0

請張貼整個Reminder.class,爲您發佈犯規編譯現在 – 2013-05-09 12:31:12

回答

2

你需要在你的web.xml給予提醒類像「com.example.Reminder」的完全限定名稱,除非你的類在默認包。

如果它在默認包,那麼有可能是另一個問題。

+0

這是在默認包的源代碼。 – sree127 2013-05-09 11:17:02

+0

凡「在contextInitialized方法迴應」變量初始化?你確定這不是空? – 2013-05-09 11:22:44

+0

+1; @ user1640534:只是不使用默認的包,TS不鼓勵使用,並導致一些副作用在Tomcat中。 – home 2013-05-09 12:13:17