// // ======================================================================== // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. // ------------------------------------------------------------------------ // All rights reserved. This program and the accompanying materials // are made available under the terms of the Eclipse Public License v1.0 // and Apache License v2.0 which accompanies this distribution. // // The Eclipse Public License is available at // http://www.eclipse.org/legal/epl-v10.html // // The Apache License v2.0 is available at // http://www.opensource.org/licenses/apache2.0.php // // You may elect to redistribute this code under either of these licenses. // ======================================================================== // package org.eclipse.jetty.demo; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.net.URLClassLoader; import javax.servlet.jsp.JspFactory; import com.acme.DateServlet; import org.apache.jasper.runtime.JspFactoryImpl; import org.apache.tomcat.InstanceManager; import org.apache.tomcat.SimpleInstanceManager; import org.eclipse.jetty.jsp.JettyJspServlet; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.servlet.DefaultServlet; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.webapp.Configuration; import org.slf4j.bridge.SLF4JBridgeHandler; /** * Example of using JSP's with embedded jetty and using a * lighter-weight ServletContextHandler instead of a WebAppContext. * * This example is somewhat odd in that it uses custom tag libs which reside * in a WEB-INF directory, even though WEB-INF is not meaningful to * a ServletContextHandler. This just shows that once we have * properly initialized the jsp engine, you can even use this type of * custom taglib, even if you don't have a full-fledged webapp. */ public class Main { static { // Setup java.util.logging to slf4j bridge SLF4JBridgeHandler.install(); } // Resource path pointing to where the WEBROOT is private static final String WEBROOT_INDEX = "/webroot/"; public static void main(String[] args) throws Exception { int port = 8080; Main main = new Main(port); main.start(); main.waitForInterrupt(); } private int port; private Server server; public Main(int port) { this.port = port; } public void start() throws Exception { server = new Server(); // Define ServerConnector ServerConnector connector = new ServerConnector(server); connector.setPort(port); server.addConnector(connector); // Add annotation scanning (for WebAppContexts) Configuration.ClassList classlist = Configuration.ClassList .setServerDefault(server); classlist.addBefore( "org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration"); // Base URI for servlet context URI baseUri = getWebRootResourceUri(); // Create Servlet context ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); servletContextHandler.setContextPath("/"); servletContextHandler.setResourceBase(baseUri.toASCIIString()); // Since this is a ServletContextHandler we must manually configure JSP support. enableEmbeddedJspSupport(servletContextHandler); // Add Application Servlets servletContextHandler.addServlet(DateServlet.class, "/date/"); // Create Example of mapping jsp to path spec ServletHolder holderAltMapping = new ServletHolder(); holderAltMapping.setName("foo.jsp"); holderAltMapping.setForcedPath("/test/foo/foo.jsp"); servletContextHandler.addServlet(holderAltMapping, "/test/foo/"); // Default Servlet (always last, always named "default") ServletHolder holderDefault = new ServletHolder("default", DefaultServlet.class); holderDefault.setInitParameter("resourceBase", baseUri.toASCIIString()); holderDefault.setInitParameter("dirAllowed", "true"); servletContextHandler.addServlet(holderDefault, "/"); server.setHandler(servletContextHandler); // Start Server // server.setDumpAfterStart(true); server.start(); } /** * Setup JSP Support for ServletContextHandlers. *

* NOTE: This is not required or appropriate if using a WebAppContext. *

* * @param servletContextHandler the ServletContextHandler to configure * @throws IOException if unable to configure */ private void enableEmbeddedJspSupport(ServletContextHandler servletContextHandler) throws IOException { // Establish Scratch directory for the servlet context (used by JSP compilation) File tempDir = new File(System.getProperty("java.io.tmpdir")); File scratchDir = new File(tempDir.toString(), "embedded-jetty-jsp"); if (!scratchDir.exists()) { if (!scratchDir.mkdirs()) { throw new IOException("Unable to create scratch directory: " + scratchDir); } } servletContextHandler.setAttribute("javax.servlet.context.tempdir", scratchDir); // Setup Default implementation JspFactory.setDefaultFactory(new JspFactoryImpl()); // Set Classloader of Context to be sane (needed for JSTL) // JSP requires a non-System classloader, this simply wraps the // embedded System classloader in a way that makes it suitable // for JSP to use ClassLoader jspClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader()); servletContextHandler.setClassLoader(jspClassLoader); // Manually call JettyJasperInitializer on context startup servletContextHandler.addBean(new EmbeddedJspStarter(servletContextHandler)); // Create / Register JSP Servlet (must be named "jsp" per spec) ServletHolder holderJsp = new ServletHolder("jsp", JettyJspServlet.class); holderJsp.setInitOrder(0); holderJsp.setInitParameter("scratchdir", scratchDir.toString()); holderJsp.setInitParameter("logVerbosityLevel", "DEBUG"); holderJsp.setInitParameter("fork", "false"); holderJsp.setInitParameter("xpoweredBy", "false"); holderJsp.setInitParameter("compilerTargetVM", "1.8"); holderJsp.setInitParameter("compilerSourceVM", "1.8"); holderJsp.setInitParameter("keepgenerated", "true"); servletContextHandler.addServlet(holderJsp, "*.jsp"); servletContextHandler.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager()); } private URI getWebRootResourceUri() throws FileNotFoundException, URISyntaxException { URL indexUri = this.getClass().getResource(WEBROOT_INDEX); if (indexUri == null) { throw new FileNotFoundException("Unable to find resource " + WEBROOT_INDEX); } // Points to wherever /webroot/ (the resource) is return indexUri.toURI(); } public void stop() throws Exception { server.stop(); } /** * Cause server to keep running until it receives a Interrupt. *

* Interrupt Signal, or SIGINT (Unix Signal), is typically seen as a result of a kill -TERM {pid} or Ctrl+C * * @throws InterruptedException if interrupted */ public void waitForInterrupt() throws InterruptedException { server.join(); } }