无法部署项目

时间:2014-08-01 15:38:27

标签: java servlets web-applications netbeans glassfish

我有数据库连接,我通过正确配置的侦听器类配置     在DD

    <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

   <listener>
        <listener-class>contpack.startL</listener-class>
    </listener>
    <context-param>
        <param-name>username</param-name>
        <param-value>root</param-value>
    </context-param>
    <context-param>
        <param-name>password</param-name>
        <param-value>root</param-value>
    </context-param>
    <context-param>
        <param-name>url</param-name>
        <param-value>jdbc:mysql://localhost:3306/mysql</param-value>
    </context-param>

    <servlet>
        <servlet-name>hell</servlet-name>
        <servlet-class>com.contpack.Dbc</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hell</servlet-name>
        <url-pattern>/Dbc</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>home.html</welcome-file>
    </welcome-file-list>
</web-app>

但仍然我无法部署项目和服务器日志显示找不到startL.java监听器类为什么这样,因为配置是正确的名称 监听器类是:

        package com.contpack;

        import javax.servlet.*;
        import javax.servlet.http.*;
        import model.Database;


        public class startL implements ServletContextListener {

        public void contextInitialized(ServletContextEvent event)
        {
                ServletContext sc = event.getServletContext();
                String url = sc.getInitParameter("url");
                String user_name = sc.getInitParameter("username");
                String password = sc.getInitParameter("password");
             //   String database = sc.getInitParameter("database");
                Database db = new Database(url,user_name,password);
            //System.out.println("in the listener!!");
                sc.setAttribute("db", db);

        }

        public void contextDestroyed(ServletContextEvent event)
        {

        }
    }

服务器日志的堆栈跟踪是:

    [2014-08-01T20:53:59.112+0530] [glassfish 4.0] [SEVERE] [] [javax.enterprise.system.core] 
    [tid: _ThreadID=34 _ThreadName=admin-listener(3)]
    [timeMillis: 1406906639112] [levelValue: 1000]
    [[Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: java.lang.ClassNotFoundException: contpack.startL.java]]

如果我删除了Listener标记,那么app运行正常

1 个答案:

答案 0 :(得分:1)

...
<listener>
    <listener-class>contpack.startL</listener-class>
</listener>
...

并且您的监听器位于包

package com.contpack;

这意味着您的侦听器完全限定名称是

com.contpack.startL

因此设置正确的侦听器类会有所帮助。您的web.xml应如下所示

...
<listener>
    <listener-class>com.contpack.startL</listener-class>
</listener>
...

遵循java命名约定仍然是一个好主意