public static void main(String[] args) throws Exception { String weppAppHome = args[0]; Integer port = 8080; Server server = new Server(port); WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/"); webapp.setCompactPath(true); webapp.setDescriptor(weppAppHome + "/WEB-INF/web.xml"); webapp.setResourceBase(weppAppHome); webapp.setParentLoaderPriority(true); server.setHandler(webapp); server.start(); server.join(); }
Server server = new Server(); // Note: if you don't want control over type of connector, etc. you can simply // call new Server(<port>); ServerConnector connector = new ServerConnector(server); connector.setHost("0.0.0.0"); connector.setPort(8085); // Setting the name allows you to serve different app contexts from different connectors. connector.setName("main"); server.addConnector(connector); WebAppContext context = new WebAppContext(); context.setContextPath("/"); // For development within an IDE like Eclipse, you can directly point to the web.xml context.setWar("src/main/webapp"); context.addFilter(MyFilter.class, "/", 1); HandlerCollection collection = new HandlerCollection(); RequestLogHandler rlh = new RequestLogHandler(); // Slf4j - who uses anything else? Slf4jRequestLog requestLog = new Slf4jRequestLog(); requestLog.setExtended(false); rlh.setRequestLog(requestLog); collection.setHandlers(new Handler[] { context, rlh }); server.setHandler(collection); try { server.start(); server.join(); } catch (Exception e) { // Google guava way throw Throwables.propagate(e); }
static WebAppContext newWebAppContext() throws MalformedURLException { final WebAppContext handler = new WebAppContext(); handler.setContextPath("/"); handler.setBaseResource(Resource.newResource(webAppRoot())); handler.setClassLoader(new URLClassLoader( new URL[] { Resource.newResource(new File(webAppRoot(), "WEB-INF" + File.separatorChar + "lib" + File.separatorChar + "hello.jar")).getURI().toURL() }, JettyService.class.getClassLoader())); handler.addBean(new ServletContainerInitializersStarter(handler), true); handler.setAttribute( "org.eclipse.jetty.containerInitializers", Collections.singletonList(new ContainerInitializer(new JettyJasperInitializer(), null))); return handler; }
/** * Create the web context for the application of specified name */ WebAppContext createWebAppContext(Builder b) { WebAppContext ctx = new WebAppContext(); setContextAttributes(ctx.getServletContext(), b.contextAttrs); ctx.setDisplayName(b.name); ctx.setContextPath("/"); ctx.setWar(appDir + "/" + b.name); return ctx; }
private WebAppContext createWebAppContext() { webAppContext = new WebAppContext(); webAppContext.setDefaultsDescriptor(GoWebXmlConfiguration.configuration(getWarFile())); webAppContext.setConfigurationClasses(new String[]{ WebInfConfiguration.class.getCanonicalName(), WebXmlConfiguration.class.getCanonicalName(), JettyWebXmlConfiguration.class.getCanonicalName() }); webAppContext.setContextPath(systemEnvironment.getWebappContextPath()); // delegate all logging to parent classloader to avoid initialization of loggers in multiple classloaders webAppContext.addSystemClass("org.apache.log4j."); webAppContext.addSystemClass("org.slf4j."); webAppContext.addSystemClass("org.apache.commons.logging."); webAppContext.setWar(getWarFile()); webAppContext.setParentLoaderPriority(systemEnvironment.getParentLoaderPriority()); return webAppContext; }
protected static void startServers(String port) throws Exception { server = new org.eclipse.jetty.server.Server(Integer.parseInt(port)); WebAppContext webappcontext = new WebAppContext(); String contextPath = null; try { contextPath = JAXRSClientServerWebSocketSpringWebAppTest.class .getResource("/jaxrs_websocket").toURI().getPath(); } catch (URISyntaxException e1) { e1.printStackTrace(); } webappcontext.setContextPath("/webapp"); webappcontext.setWar(contextPath); HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers(new Handler[] {webappcontext, new DefaultHandler()}); server.setHandler(handlers); server.start(); }
public static Server createServer(int port) throws MalformedURLException, URISyntaxException { Server server = new Server(); server.setStopAtShutdown(true); ServerConnector connector = new ServerConnector(server); connector.setPort(port); connector.setReuseAddress(true); server.setConnectors(new Connector[]{connector}); WebAppContext webContext = new WebAppContext("webapp", CONTEXT); webContext.setBaseResource(Resource.newResource(new URL(ApiWebSite.class.getResource("/webapp/WEB-INF"), "."))); webContext.setClassLoader(ApiWebSite.class.getClassLoader()); server.setHandler(webContext); return server; }
Server server = new Server(8080); server.setStopAtShutdown(true); WebAppContext webAppContext = new WebAppContext(); webAppContext.setContextPath("/app"); webAppContext.setResourceBase("src/main/webapp"); webAppContext.setClassLoader(getClass().getClassLoader()); server.addHandler(webAppContext); server.start(); URL url = new URL("http://localhost:8080/app/some_call"); URLConnection connection = url.openConnection(); List<String> lines = IOUtils.readLines(connection.getInputStream()); System.out.println(lines.get(0));
Server server = new Server(8080); URL webRootLocation = this.getClass().getResource("/app.html"); if (webRootLocation == null) { throw new IllegalStateException("Unable to determine webroot URL location"); } URI webRootUri = URI.create(webRootLocation.toURI().toASCIIString().replaceFirst("/app.html$","/")); WebAppContext altHandler = new WebAppContext(); altHandler.setResourceBase(Resource.newResource(webRootUri).toString()); altHandler.setDescriptor(Resource.newResource(webRootUri).toString() + "WEB-INF/web.xml"); altHandler.setContextPath("/"); altHandler.setParentLoaderPriority(true); server.setHandler(altHandler); server.start(); server.join(); }
public static void main(String[] args) throws Exception { Server server = new Server(8080); String rootPath = SimplestServer.class.getClassLoader().getResource(".").toString(); WebAppContext webapp = new WebAppContext(rootPath + "../../src/main/webapp", ""); server.setHandler(webapp); server.start(); server.join(); }
final WebAppContext staticContext = new WebAppContext(); staticContext.setServer( getJetty() ); staticContext.setContextPath( mountPoint ); staticContext.setSessionHandler( sessionHandler ); staticContext.setInitParameter( "org.eclipse.jetty.servlet.Default.dirAllowed", "false" ); URL resourceLoc = getClass().getClassLoader().getResource( contentLocation ); if ( resourceLoc != null ) URL url = resourceLoc.toURI().toURL(); final Resource resource = Resource.newResource( url ); staticContext.setBaseResource( resource ); staticContext.addFilter( new FilterHolder( new StaticContentFilter() ), "/*", EnumSet.of( DispatcherType.REQUEST, DispatcherType.FORWARD ) ); handlers.addHandler( staticContext );
server = (Server) new XmlConfiguration(jettyInputStream).configure(); } else { server = new Server(port); server = new Server(port); webAppContext = new WebAppContext(); webAppContext.setParentLoaderPriority(true); webAppContext.setThrowUnavailableOnStartupException(true); classPath = classLoader.getResource(".").getPath(); rootPath = new File(classPath).getParentFile().getParentFile().getCanonicalFile().getAbsolutePath() + "/"; webAppContext.setDescriptor(webXmlPath); webAppContext.setContextPath(contextPath); webAppContext.setResourceBase(webappUrl); webAppContext.setBaseResource(new ResourceCollection(resourceUrls.toArray(new String[resourceUrls.size()]))); webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false"); webAppContext.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false"); SessionManager sessionManager = webAppContext.getSessionHandler().getSessionManager(); sessionManager.setSessionIdPathParameterName(null); server.setHandler(webAppContext);
public static void main(String[] args) throws Exception { String configPort = "8080"; if(args.length > 0){ configPort = args[0]; } String port = System.getProperty("server.http.port", configPort); System.out.println("Server started on port: " + port); Server server = new Server(); server.setConnectors(new Connector[]{createConnector(server, Integer.parseInt(port))}); ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS); context.setContextPath("/api"); ServletHolder servletHolder = new ServletHolder(createJerseyServlet()); servletHolder.setInitOrder(1); context.addServlet(servletHolder, "/*"); WebAppContext webAppContext = new WebAppContext(); webAppContext.setServer(server); webAppContext.setContextPath("/"); String resourceBase = System.getProperty("resourceBase"); if(resourceBase != null){ webAppContext.setResourceBase(resourceBase); } else { webAppContext.setResourceBase(Main.class.getResource("/webapp").toExternalForm()); } HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[]{context, webAppContext}); server.setHandler(handlers); server.start(); server.join(); }
this.consoleSourceLocations=consoleSourceLocations; this.revertProperties=revertProperties; server=new Server(); server.addConnector(connector); context = new WebAppContext(); context.setResourceBase(webapp.getAbsolutePath()); context.setContextPath("/"); context.setClassLoader(new WebAppClassLoader(classLoader, context)); context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*/.+\\.jar$|.*/classes/.*"); URLClassLoader ucl = (URLClassLoader) classLoader; for(URL url: ucl.getURLs()){ File file = new File(url.getFile()); if(file.exists()){ context.getMetaData().addWebInfJar(Resource.newResource(file)); server.setHandler(context);
String path = u.getPath(); if (path == null) { throw new IllegalArgumentException("The URI path, of the URI " + u + ", must be non-null"); path = String.format("/%s", UriComponent.decodePath(u.getPath(), true).get(1).toString()); WebAppContext context = new WebAppContext(); context.setDisplayName("JettyContext"); context.setContextPath(path); context.setConfigurations(new Configuration[]{new WebXmlConfiguration()}); ServletHolder holder; if (c != null) { holder = context.addServlet(c, "/*"); } else { holder = new ServletHolder(servlet); context.addServlet(holder, "/*"); context.setInitParameter(e.getKey(), e.getValue()); server.setHandler(context); server.start(); return server;
final Server server = new Server(); //skipped socketconnector initialization final WebAppContext bb = new WebAppContext(); bb.setServer(server); bb.setContextPath("/"); bb.setWar("src/main/webapp"); final URL url = new File("target/classes").getAbsoluteFile().toURI().toURL(); final Resource resource = new FileResource(url); final ResourceHandler handler = new ResourceHandler(); handler.setBaseResource(resource); server.addHandler(handler); server.addHandler(bb); server.start();
protected void run() { //System.out.println("Starting Server"); server = new org.eclipse.jetty.server.Server(Integer.parseInt(PORT)); WebAppContext webappcontext = new WebAppContext(); webappcontext.setContextPath("/"); webappcontext.setBaseResource(Resource.newClassPathResource("/webapp")); server.setHandler(new HandlerCollection(webappcontext, new DefaultHandler())); try { server.start(); } catch (Exception e) { throw new RuntimeException(e); } } public void tearDown() throws Exception {
public static void main(String[] args) throws Exception { Server server = new Server(8080); Path warPath = new File("webapps/hello.war").toPath().toRealPath(); System.err.println("WAR File is " + warPath); WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/"); webapp.setWar(warPath.toUri().toASCIIString()); webapp.setParentLoaderPriority(true); server.setHandler(webapp); server.start(); server.join(); } }
Server server = new Server(); WebAppContext servletContextHandler = new WebAppContext(); servletContextHandler.setContextPath("/"); servletContextHandler.setResourceBase("src/main/resources/" + webFolder); ClassList clist = ClassList.setServerDefault(server); clist.addBefore(JettyWebXmlConfiguration.class.getName(), AnnotationConfiguration.class.getName()); servletContextHandler.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", ".*(/target/classes/|.*.jar)"); servletContextHandler.setParentLoaderPriority(true); servletContextHandler.setInitParameter("useFileMappedBuffer", "false"); servletContextHandler.setAttribute("globalProps", globalProps); Resource staticResources = Resource.newClassPathResource(webFolder); staticResourceHandler.setBaseResource(staticResources); staticResourceHandler.setWelcomeFiles(new String[]{"html/index.html"}); handlers.setHandlers(new Handler[]{staticResourceHandler, servletContextHandler}); server.setHandler(handlers); HttpConfiguration http_config = new HttpConfiguration(); http_config.setSecureScheme("https"); server.setConnectors(new Connector[]{https}); } else { if(globalProps.getProperty("X-Forwarded-For").equals("true")) {
private static void startServer() throws Exception { File warFile = findWar(); server = new Server(8080); WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/"); webapp.setWar(warFile.getAbsolutePath()); server.setHandler(webapp); server.start(); eurekaServiceUrl = "http://localhost:8080/v2"; }