4

我已经创建了 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);
   }
 }
4

5 回答 5

10

您正在做的是混淆<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 
于 2013-09-19T10:16:51.277 回答
1

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

<listener>
        <listener-class>com.somePackage.ListenerClass</listener-class>
</listener> 
于 2013-09-19T10:17:49.687 回答
1

侦听器标记应在 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>
于 2013-09-19T10:22:09.110 回答
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> 
于 2013-09-19T10:17:35.973 回答
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);
    }

}

享受编码:)

于 2018-06-28T10:58:44.190 回答