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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18891903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:59:26  来源:igfitidea点击:

How do I put listener in web.xml java?

javalistener

提问by Karen Goh

I have created web application and I'd like to know where do I put my listener in the web.xml.

我已经创建了 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>

The error message I received is:

我收到的错误信息是:

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]

Here's my config file:

这是我的配置文件:

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

采纳答案by Suresh Atta

What you are doing is you are mixing up the <servlet>and <listener>tags .They should be seperate.

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

That should be

那应该是

<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>

or

或者

<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>

And In your class

在你的课堂上

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

As a side note:

作为旁注:

In Java, class names start's with capital letters. public class configshould be

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

public class Config 

回答by Philipp Sander

as you see in the content model of wep-app:

正如您在 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)*

it is a sibling of servletnot a child:

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> 

回答by Prasad Kharkar

Listener should not be within a servlettag and provide fully qualified name

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

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

回答by Deepak

Listener tag should be defined under web-app tag.

监听器标签应该在 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>

回答by insung

web.xml would be

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>

and com.my.ServletContextClass.java would be

和 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);
    }

}

enjoy coding :)

享受编码:)