title: ServletContainerInitializer vs ServletContextListener
body: <p>I'm trying to register a servlet using servletContainerInitializer but it doesn't seem to work, Maybe it's my code (kindly review it), but I came to wonder about the difference between <a href="http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContainerInitializer.html" rel="noreferrer"><code>ServletContainerInitializer</code></a> and <a href="http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContextListener.html" rel="noreferrer"><code>ServletContextListener</code></a>, because the follwing code runs fine when used as <code>ServletContextListener</code> instead.</p>
  <p>From servlet 3.0 specification:</p>
  <blockquote>
  <h2>4.4</h2>
  <h3>Configuration methods (adding servlets dynamically):</h3>
  <p>... or from the <code>onStartup</code> method of a <code>ServletContainerInitializer</code> implementation ...</p>
  </blockquote>
  <p>The <code>ServletContainerInitializer</code>:</p>
  <pre><code>package com.marmoush.javaexamples.nullhaus.servlet;
  
  import java.util.Set;
  import javax.servlet.ServletContainerInitializer;
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRegistration;
  
  public class MyInit implements ServletContainerInitializer {
      public void onStartup(Set&lt;Class&lt;?&gt;&gt; c, ServletContext ctx) throws ServletException {
          System.out.println(&quot;hello&quot;);
          ServletRegistration reg = ctx.addServlet(&quot;q31&quot;,&quot;com.marmoush.javaexamples.nullhaus.servlet.Q31&quot;);
          reg.addMapping(&quot;/q31/*&quot;);
      }
  }
  </code></pre>
  <p>The servlet which I'm trying to auto-register:</p>
  <pre><code>package com.marmoush.javaexamples.nullhaus.servlet;
  
  import java.io.IOException;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  public class Q31 extends HttpServlet {
      @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          resp.getWriter().println(&quot;hello world&quot;);
      }
  }
  </code></pre>
  <p>Original code from nullhaus java examples website &quot;only class name edited&quot; also didn't work!</p>
  <pre><code>package com.marmoush.javaexamples.nullhaus.servlet;
  
  import java.util.Set;
  
  import javax.servlet.Servlet;
  import javax.servlet.ServletContainerInitializer;
  import javax.servlet.ServletContext;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRegistration;
  
  public class MyInit implements ServletContainerInitializer {
      public void onStartup(Set&lt;Class&lt;?&gt;&gt; c, ServletContext ctx) throws ServletException {
          try {
              Class klass = Class.forName(&quot;com.marmoush.javaexamples.nullhaus.servlet.Q31&quot;);
              Class&lt;Q31&gt; clazz = (Class&lt;Q31&gt;) klass;
              Servlet s = ctx.createServlet(clazz);
              ServletRegistration.Dynamic d = ctx.addServlet(&quot;q31&quot;, s);
              d.addMapping(&quot;/baz/*&quot;);
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>