1 // 2 // ======================================================================== 3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd. 4 // ------------------------------------------------------------------------ 5 // All rights reserved. This program and the accompanying materials 6 // are made available under the terms of the Eclipse Public License v1.0 7 // and Apache License v2.0 which accompanies this distribution. 8 // 9 // The Eclipse Public License is available at 10 // http://www.eclipse.org/legal/epl-v10.html 11 // 12 // The Apache License v2.0 is available at 13 // http://www.opensource.org/licenses/apache2.0.php 14 // 15 // You may elect to redistribute this code under either of these licenses. 16 // ======================================================================== 17 // 18 19 package org.eclipse.jetty.embedded; 20 21 import java.io.File; 22 import java.lang.management.ManagementFactory; 23 24 import org.eclipse.jetty.jmx.MBeanContainer; 25 import org.eclipse.jetty.security.HashLoginService; 26 import org.eclipse.jetty.server.Server; 27 import org.eclipse.jetty.webapp.Configuration; 28 import org.eclipse.jetty.webapp.WebAppContext; 29 30 public class OneWebAppWithJsp 31 { 32 public static void main( String[] args ) throws Exception 33 { 34 // Create a basic jetty server object that will listen on port 8080. 35 // Note that if you set this to port 0 then 36 // a randomly available port will be assigned that you can either look 37 // in the logs for the port, 38 // or programmatically obtain it for use in test cases. 39 Server server = new Server( 8080 ); 40 41 // Setup JMX 42 MBeanContainer mbContainer = new MBeanContainer( 43 ManagementFactory.getPlatformMBeanServer() ); 44 server.addBean( mbContainer ); 45 46 // The WebAppContext is the entity that controls the environment in 47 // which a web application lives and 48 // breathes. In this example the context path is being set to "/" so it 49 // is suitable for serving root context 50 // requests and then we see it setting the location of the war. A whole 51 // host of other configurations are 52 // available, ranging from configuring to support annotation scanning in 53 // the webapp (through 54 // PlusConfiguration) to choosing where the webapp will unpack itself. 55 WebAppContext webapp = new WebAppContext(); 56 webapp.setContextPath( "/" ); 57 File warFile = new File( 58 "../../jetty-distribution/target/distribution/demo-base/webapps/test.war" ); 59 if (!warFile.exists()) 60 { 61 throw new RuntimeException( "Unable to find WAR File: " 62 + warFile.getAbsolutePath() ); 63 } 64 webapp.setWar( warFile.getAbsolutePath() ); 65 66 // This webapp will use jsps and jstl. We need to enable the 67 // AnnotationConfiguration in order to correctly 68 // set up the jsp container 69 Configuration.ClassList classlist = Configuration.ClassList 70 .setServerDefault( server ); 71 classlist.addBefore( 72 "org.eclipse.jetty.webapp.JettyWebXmlConfiguration", 73 "org.eclipse.jetty.annotations.AnnotationConfiguration" ); 74 75 // Set the ContainerIncludeJarPattern so that jetty examines these 76 // container-path jars for tlds, web-fragments etc. 77 // If you omit the jar that contains the jstl .tlds, the jsp engine will 78 // scan for them instead. 79 webapp.setAttribute( 80 "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", 81 ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/[^/]*taglibs.*\\.jar$" ); 82 83 // A WebAppContext is a ContextHandler as well so it needs to be set to 84 // the server so it is aware of where to 85 // send the appropriate requests. 86 server.setHandler( webapp ); 87 88 // Configure a LoginService. 89 // Since this example is for our test webapp, we need to setup a 90 // LoginService so this shows how to create a very simple hashmap based 91 // one. The name of the LoginService needs to correspond to what is 92 // configured in the webapp's web.xml and since it has a lifecycle of 93 // its own we register it as a bean with the Jetty server object so it 94 // can be started and stopped according to the lifecycle of the server 95 // itself. 96 HashLoginService loginService = new HashLoginService(); 97 loginService.setName( "Test Realm" ); 98 loginService.setConfig( "src/test/resources/realm.properties" ); 99 server.addBean( loginService ); 100 101 // Start things up! 102 server.start(); 103 104 // The use of server.join() the will make the current thread join and 105 // wait until the server is done executing. 106 // See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join() 107 server.join(); 108 } 109 }