title: Embedded Jetty + ShiroFilter problems
body: <p>I haven't asked questions here yet, but after around 2 months of struggle on this, I decided to finally ask for help.</p>
  
  <p>I am new to Java, this is actually my first project (just for fun), in which I am trying to create a web server with Jetty (without a web.xml descriptor) and Shiro authentication (with shiro.ini configuration file), but for some reason I cannot make it work. I tried pretty much everything I found on the internet, tried starting over, Read all The FMs... But nothing...</p>
  
  <p>The final situation is that I get errors about no EnvironmentListener registered:</p>
  
  <pre><code>javax.servlet.ServletException: java.lang.IllegalStateException: No WebEnvironment found: no EnvironmentLoaderListener registered?
      at org.apache.shiro.web.servlet.AbstractFilter.init(AbstractFilter.java:105)
      at org.eclipse.jetty.servlet.FilterHolder.initialize(FilterHolder.java:138)
      at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:852)
      at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:341)
      at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:742)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
      at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
      at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
      at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
      at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:132)
      at org.eclipse.jetty.server.Server.start(Server.java:399)
      at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:114)
      at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:61)
      at org.eclipse.jetty.server.Server.doStart(Server.java:366)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
      at MyPackage.Main.main(Main.java:89)
  </code></pre>
  
  <p>So after a lot of reading, I ended up with this addition to the code:</p>
  
  <pre><code>EnvironmentLoaderListener listener = new EnvironmentLoaderListener();
  secured.callContextInitialized(listener, new ServletContextEvent(secured.getServletContext()));
  </code></pre>
  
  <p>I also had a different error before, related to SecurityManager not set (or something), but for some reason, I don't get that error now?</p>
  
  <p>To fix it, I used that code (it is commented now, as apparently I don't need it anymore, but anyways, at this point it makes no difference if it is commented or not...)</p>
  
  <pre><code>    //Factory&lt;SecurityManager&gt; factory=new IniSecurityManagerFactory("src/main/resources/shiro.ini");
      //SecurityManager securityManager=factory.getInstance();
      //SecurityUtils.setSecurityManager(securityManager);
  </code></pre>
  
  <p>So after all, I ended up with this error:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at org.eclipse.jetty.server.handler.ContextHandler$Context.log(ContextHandler.java:2059)
      at org.apache.shiro.web.env.EnvironmentLoader.initEnvironment(EnvironmentLoader.java:127)
      at org.apache.shiro.web.env.EnvironmentLoaderListener.contextInitialized(EnvironmentLoaderListener.java:58)
      at org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized(ContextHandler.java:801)
      at org.eclipse.jetty.servlet.ServletContextHandler.callContextInitialized(ServletContextHandler.java:499)
      at MyPackage.Main.main(Main.java:72)
  </code></pre>
  
  <p>This is my code, I guess it has much to optimize, but as I am a newbie to Java, I am open to any suggestions...</p>
  
  <pre><code>import java.util.EnumSet;
  import javax.servlet.DispatcherType;
  import javax.servlet.ServletContextEvent;
  import org.apache.jasper.servlet.JspServlet;
  import org.apache.log4j.PropertyConfigurator;
  import org.apache.shiro.web.env.EnvironmentLoaderListener;
  import org.apache.shiro.web.servlet.ShiroFilter;
  import org.eclipse.jetty.server.Server;
  import org.eclipse.jetty.server.handler.HandlerCollection;
  import org.eclipse.jetty.servlet.DefaultServlet;
  import org.eclipse.jetty.servlet.ServletContextHandler;
  import org.eclipse.jetty.servlet.ServletHolder;
  
  public class Main {
  
      public static void main(String[] args) {
          System.out.println("Initializing server...");
          PropertyConfigurator.configure("config//log4j.properties");
  
          final ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
  
          context.setContextPath("/");
          context.setResourceBase("src/main/webapp/main");
          context.setClassLoader(Thread.currentThread().getContextClassLoader());
  
          context.addServlet(DefaultServlet.class, "/*");
  
          context.addServlet("MyPackage.CheckLogin", "/checklogin");
  
          final ServletHolder jsp = context.addServlet(JspServlet.class, "*.jsp");
          jsp.setInitParameter("classpath", context.getClassPath());
  
          final ServletContextHandler secured = new ServletContextHandler(ServletContextHandler.SESSIONS);
  
          secured.setContextPath("/protected");
          secured.setResourceBase("src/main/webapp/protected");
          secured.setClassLoader(Thread.currentThread().getContextClassLoader());
  
          secured.addServlet(DefaultServlet.class, "/*");
  
          final ServletHolder jsp2 = secured.addServlet(JspServlet.class, "*.jsp");
          jsp2.setInitParameter("classpath", context.getClassPath());
  
          EnvironmentLoaderListener listener = new EnvironmentLoaderListener();
          secured.callContextInitialized(listener, new ServletContextEvent(secured.getServletContext()));  
  
          //Factory&lt;SecurityManager&gt; factory=new IniSecurityManagerFactory("src/main/resources/shiro.ini");
          //SecurityManager securityManager=factory.getInstance();
          //SecurityUtils.setSecurityManager(securityManager);
  
          secured.addFilter(ShiroFilter.class,"/protected/*",EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD,DispatcherType.INCLUDE,DispatcherType.ERROR));
  
          HandlerCollection hc = new HandlerCollection();
          hc.addHandler(secured);
          hc.addHandler(context);
  
          final Server server = new Server(8080);     
          server.setHandler(hc);
  
          System.out.println("Starting server...");
          try {
              server.start();
          } catch(Exception e) {
              System.out.println("Failed to start server!");
              e.printStackTrace();
              return;
          }
  
          System.out.println("Server running...");
          while(true) {
              try {
                  server.join();
              } catch(InterruptedException e) {
                  System.out.println("Server interrupted!");
              }
          }
      }
  }
  </code></pre>
  
  <p>I pretty much tried everything I could figure out, so please help me fix it, as it is now driving me crazy...</p>
  
  <p>If any additional information is needed, please let me know and I will provide it :)</p>
  
  <p>edit: I tried to go through all the listed classes in the stack trace, but after a few hours spent in reading the different java classes and functions in them, I got into a dead end...</p>
  
  <p>Jetty jar version: jetty-all-9.3.0-M2</p>