title: How do I put listener in web.xml java?
body: <p>I have created web application and I'd like to know where do I put my listener in the web.xml.</p>
  
  <pre><code>  &lt;servlet&gt;
      &lt;servlet-name&gt;ProcessReg&lt;/servlet-name&gt;
      &lt;servlet-class&gt;ProcessReg&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;pract123&lt;/param-name&gt;
          &lt;param-value&gt;jdbc:odbc:practODBC&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;config&lt;/listener-class&gt;
      &lt;/listener&gt;         
    &lt;/servlet&gt;
  </code></pre>
  
  <p>The error message I received is:</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>Here's my config file:</p>
  
  <pre><code>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);
     }
   }
  </code></pre>