如何将监听器放在web.xml中?

时间:2013-09-19 10:11:38

标签: java listener

我已经创建了Web应用程序,我想知道在哪里将我的监听器放在web.xml中。

  <servlet>
    <servlet-name>ProcessReg</servlet-name>
    <servlet-class>ProcessReg</servlet-class>
    <init-param>
        <param-name>pract123</param-name>
        <param-value>jdbc:odbc:practODBC</param-value>
    </init-param>
    <listener>
        <listener-class>config</listener-class>
    </listener>         
  </servlet>

我收到的错误消息是:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'listener'. One of '{"http://java.sun.com/xml/ns/javaee":init-param, "http://java.sun.com/xml/ns/javaee":load-on-startup, "http://java.sun.com/xml/ns/javaee":run-as, "http://java.sun.com/xml/ns/javaee":security-role-ref}' is expected. [17]

这是我的配置文件:

public class config implements ServletContextListener {
private static final String ATTRIBUTE_NAME = "config";
private DataSource dataSource;

@Override
public void contextInitialized(ServletContextEvent event) {
    ServletContext servletContext = event.getServletContext();
    String databaseName = servletContext.getInitParameter("pract123");
    try {
           dataSource = (DataSource) new    InitialContext().lookup("java:/comp      /env/jdbc/TestDB");  
    } catch (NamingException e) {
        throw new RuntimeException("Config failed: datasource not found", e);
    }}

@Override
public void contextDestroyed(ServletContextEvent event) {
    // NOOP.
}

public DataSource getDataSource() {
    return dataSource;
}

public static config getInstance(ServletContext servletContext) {
    return (config) servletContext.getAttribute(ATTRIBUTE_NAME);
   }
 }

5 个答案:

答案 0 :(得分:7)

您正在做的是混合<servlet><listener>标签。它们应该是分开的。

那应该是

<servlet>
    <servlet-name>ProcessReg</servlet-name>
    <servlet-class>ProcessReg</servlet-class>
    <init-param>
        <param-name>pract123</param-name>
        <param-value>jdbc:odbc:practODBC</param-value>
    </init-param>       
  </servlet>

<listener>
        <listener-class>fully.qaulified.path.ContextListener</listener-class>
</listener>

<listener>
          <listener-class>fully.qaulified.path.ContextListener</listener-class>
 </listener>

<servlet>
    <servlet-name>ProcessReg</servlet-name>
    <servlet-class>ProcessReg</servlet-class>
    <init-param>
        <param-name>pract123</param-name>
        <param-value>jdbc:odbc:practODBC</param-value>
    </init-param>       
  </servlet>

在你的课堂上

package fully.qaulified.path;
public class ContextListener implements ServletContextListener {
..
..

作为旁注:

在Java中,类名以大写字母开头。 public class config应该是

public class Config 

答案 1 :(得分:1)

侦听器不应位于servlet标记内,并提供完全限定名称

<listener>
        <listener-class>com.somePackage.ListenerClass</listener-class>
</listener> 

答案 2 :(得分:0)

正如您在wep-app的内容模型中看到的那样:

Content Model : (((description*, display-name*, icon*)) | distributable | context-param | filter | filter-mapping | 
listener | servlet | servlet-mapping | session-config | mime-mapping | welcome-file-list | error-page | jsp-config | 
security-constraint | login-config | security-role | ((env-entry*, ejb-ref*, ejb-local-ref*, ((service-ref*)), resource-
ref*, resource-env-ref*, message-destination-ref*, persistence-context-ref*, persistence-unit-ref*, post-
construct*, pre-destroy*)) | message-destination | locale-encoding-mapping-list)*

它是servlet不是孩子的兄弟姐妹:

<servlet>
    <servlet-name>ProcessReg</servlet-name>
    <servlet-class>ProcessReg</servlet-class>
    <init-param>
        <param-name>pract123</param-name>
        <param-value>jdbc:odbc:practODBC</param-value>
    </init-param>        
</servlet>

<listener>
    <listener-class>config</listener-class>
</listener> 

答案 3 :(得分:0)

应在web-app标记下定义监听器标记。

<web-app>
   <display-name>MyListeningApplication</display-name>
   <listener>
      <listener-class>config</listener-class>
   </listener>
   <servlet-name>ProcessReg</servlet-name>
   <servlet-class>ProcessReg</servlet-class>
   <init-param>
       <param-name>pract123</param-name>
       <param-value>jdbc:odbc:practODBC</param-value>
   </init-param>
</web-app>

答案 4 :(得分:0)

web.xml将是

<listener>
    <listener-class>com.my.ServletContextClass</listener-class>
</listener>
<context-param>
    <param-name>pract123</param-name>
    <param-value>jdbc:odbc:practODBC</param-value>
</context-param>

和com.my.ServletContextClass.java应该是

public class ServletContextClass implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        String value = context.getInitParameter("pract123");
        System.out.println("value: " + value);
    }

}

享受编码:)