title: How to serve JSP files using Jetty?
body: <p>I'm embedding jetty, serving a single servlet and some static content.  I've downloaded jetty from <a href="http://download.eclipse.org/jetty/7.4.5.v20110725/dist/" rel="noreferrer">http://download.eclipse.org/jetty/7.4.5.v20110725/dist/</a>, and added all jars from JETTY_HOME/lib/* and JETTY_HOME/lib/jsp/* to user librarys in eclipe; these user librarys have been added to my project.  If I put a JSP file in my my static content folder (./webapps/static/) and view it at <a href="http://localhost:8080/static/test.jsp" rel="noreferrer">http://localhost:8080/static/test.jsp</a>, the java expression are not evaluated and I get shown the full contents of the file.</p>
  
  <p>What am I missing?</p>
  
  <p>My main java class:</p>
  
  <pre><code>import org.eclipse.jetty.server.Handler;
  import org.eclipse.jetty.server.Server;
  import org.eclipse.jetty.server.handler.ContextHandler;
  import org.eclipse.jetty.server.handler.HandlerList;
  import org.eclipse.jetty.server.handler.ResourceHandler;
  import org.eclipse.jetty.server.nio.SelectChannelConnector;
  import org.eclipse.jetty.servlet.ServletContextHandler;
  import org.eclipse.jetty.servlet.ServletHolder;
  
  public class Test {
  
      public static void main(String[] args) throws Exception {
          Server server = new Server();
          SelectChannelConnector connector = new SelectChannelConnector();
          connector.setPort(8080);
          server.addConnector(connector);               
  
          // Create a resource handler for static content.
          ResourceHandler staticResourceHandler = new ResourceHandler();
          staticResourceHandler.setResourceBase("./webapps/static/");
          staticResourceHandler.setDirectoriesListed(true);        
          //staticResourceHandler.setWelcomeFiles(new String[]{ "index.html",  });         
          //staticResourceHandler.setCacheControl("no-store,no-cache,must-revalidate");
  
          // Create context handler for static resource handler.
          ContextHandler staticContextHandler = new ContextHandler();
          staticContextHandler.setContextPath("/static");       
          staticContextHandler.setHandler(staticResourceHandler);        
  
          // Create servlet context handler for main servlet.
          ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
          servletContextHandler.setContextPath("/");       
          servletContextHandler.addServlet(new ServletHolder(new HelloServlet()),"/");
  
          // Create a handler list to store our static and servlet context handlers.
          HandlerList handlers = new HandlerList();
          handlers.setHandlers(new Handler[] { staticContextHandler, servletContextHandler });
  
          // Add the handlers to the server and start jetty.
          server.setHandler(handlers);
          server.start();
          server.join();
      }           
  
  }
  </code></pre>
  
  <p>My JSP file I want to serve:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  Time: &lt;%= new java.util.Date() %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>List of jars in JETTY_HOME/lib/:</p>
  
  <pre><code>$ ls -1 ./jetty-distribution-7.4.5.v20110725/lib/*.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-ajp-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-annotations-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-client-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-continuation-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-deploy-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-http-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-io-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-jmx-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-jndi-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-overlay-deployer-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-plus-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-policy-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-rewrite-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-security-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-server-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-servlet-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-servlets-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-util-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-webapp-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-websocket-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jetty-xml-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/servlet-api-2.5.jar
  </code></pre>
  
  <p>List of jars in JETTY_HOME/lib/jsp/:</p>
  
  <pre><code>$ ls -1 ./jetty-distribution-7.4.5.v20110725/lib/jsp/*.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jsp/com.sun.el_1.0.0.v201004190952.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jsp/ecj-3.6.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jsp/javax.el_2.1.0.v201004190952.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jsp/javax.servlet.jsp_2.1.0.v201004190952.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jsp/javax.servlet.jsp.jstl_1.2.0.v201004190952.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jsp/jetty-jsp-2.1-7.4.5.v20110725.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jsp/org.apache.jasper.glassfish_2.1.0.v201007080150.jar
  ./jetty-distribution-7.4.5.v20110725/lib/jsp/org.apache.taglibs.standard.glassfish_1.2.0.v201004190952.jar
  </code></pre>
  
  <hr>
  
  <p>Update: The suggestion from @JJ helped work out how to correctly serve JSP file with jetty.  My only issue now is how to stop jetty from listing the directory contents of ./webapps/jsp/.  The most basic fix I have for the moment is to put index.html or index.jsp in ./webapps/jsp/, but I would prefer be able to configure jetty to return a forbidden error.</p>
  
  <pre><code>import org.eclipse.jetty.server.Handler;
  import org.eclipse.jetty.server.Server;
  import org.eclipse.jetty.server.handler.ContextHandler;
  import org.eclipse.jetty.server.handler.HandlerList;
  import org.eclipse.jetty.server.handler.ResourceHandler;
  import org.eclipse.jetty.server.nio.SelectChannelConnector;
  import org.eclipse.jetty.servlet.ServletContextHandler;
  import org.eclipse.jetty.servlet.ServletHolder;
  import org.eclipse.jetty.webapp.WebAppContext;
  
  public class Test {
  
      public static void main(String[] args) throws Exception {
          Server server = new Server();
          SelectChannelConnector connector = new SelectChannelConnector();
          connector.setHost("127.0.0.1");
          connector.setPort(8080);
          server.addConnector(connector);
  
          // Create a resource handler for static content.
          ResourceHandler staticResourceHandler = new ResourceHandler();
          staticResourceHandler.setResourceBase("./webapps/static/");
          staticResourceHandler.setDirectoriesListed(true);
  
          // Create context handler for static resource handler.
          ContextHandler staticContextHandler = new ContextHandler();
          staticContextHandler.setContextPath("/static");
          staticContextHandler.setHandler(staticResourceHandler);
  
          // Create WebAppContext for JSP files.
          WebAppContext webAppContext = new WebAppContext();
          webAppContext.setContextPath("/jsp");
          webAppContext.setResourceBase("./webapps/jsp/");
          // ??? THIS DOES NOT STOP DIR LISTING OF ./webapps/jsp/ ???
          webAppContext.setInitParameter("dirAllowed", "false");   
  
          // Create servlet context handler for main servlet.
          ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
          servletContextHandler.setContextPath("/");
          servletContextHandler.addServlet(new ServletHolder(new HelloServlet()), "/*");
  
          // Create a handler list to store our static, jsp and servlet context handlers.
          HandlerList handlers = new HandlerList();
          handlers.setHandlers(new Handler[] { staticContextHandler, webAppContext, servletContextHandler });
  
          // Add the handlers to the server and start jetty.
          server.setHandler(handlers);    
          server.start();
          server.join();
      }
  
  }
  </code></pre>