String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); StandardContext ctx = (StandardContext) tomcat.addWebapp("/embeddedTomcat", new File(webappDirLocation).getAbsolutePath()); //declare an alternate location for your "WEB-INF/classes" dir: File additionWebInfClasses = new File("target/classes"); WebResourceRoot resources = new StandardRoot(ctx); resources.addPreResources(new DirResourceSet(resources, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), "/")); ctx.setResources(resources); tomcat.start(); tomcat.getServer().await();
public static void main(String[] args) throws Exception { String webappsPath = args[0]; int port = Integer.parseInt( args[1] ); File dataDir = Files.createTempDir(); dataDir.deleteOnExit(); Tomcat tomcat = new Tomcat(); tomcat.setBaseDir(dataDir.getAbsolutePath()); tomcat.setPort(port); tomcat.getConnector().setAttribute("maxThreads", "1000"); tomcat.addWebapp("/", new File(webappsPath).getAbsolutePath()); logger.info("-----------------------------------------------------------------"); logger.info("Starting Tomcat port {} dir {}", port, webappsPath); logger.info("-----------------------------------------------------------------"); tomcat.start(); while ( true ) { Thread.sleep(1000); } }
String webappDirLocation = "src/main/webapp/"; Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); StandardContext ctx = (StandardContext) tomcat.addWebapp("/embeddedTomcat", new File(webappDirLocation).getAbsolutePath()); //declare an alternate location for your "WEB-INF/classes" dir: File additionWebInfClasses = new File("target/classes"); VirtualDirContext resources = new VirtualDirContext(); resources.setExtraResourcePaths("/WEB-INF/classes=" + additionWebInfClasses); ctx.setResources(resources); tomcat.start(); tomcat.getServer().await();
@Before public void setUp() throws Exception { props.setProperty(Property.PATH_DATA.getKey(), temp.newFolder("data").getAbsolutePath()); when(tomcat.addWebapp(anyString(), anyString())).thenReturn(mock(StandardContext.class)); }
@Test public void configure_root_webapp() throws Exception { props.setProperty("foo", "bar"); StandardContext context = mock(StandardContext.class); when(tomcat.addWebapp(anyString(), anyString())).thenReturn(context); underTest.configure(tomcat, new Props(props)); // configure webapp with properties verify(context).addParameter("foo", "bar"); }
@Test public void create_dir_and_configure_static_directory() throws Exception { File dir = temp.newFolder(); dir.delete(); underTest.addStaticDir(tomcat, "/deploy", dir); assertThat(dir).isDirectory().exists(); verify(tomcat).addWebapp("/deploy", dir.getAbsolutePath()); }
@Override public void start() throws Exception { super.start(); String workingDir = System.getProperty("java.io.tmpdir"); tomcat = new Tomcat(); tomcat.setPort(8173); tomcat.setBaseDir(workingDir); tomcat.addWebapp("", webRoot.getAbsolutePath()); tomcat.start(); }
private static StandardContext addContext(Tomcat tomcat, String contextPath, File dir) { try { StandardContext context = (StandardContext) tomcat.addWebapp(contextPath, dir.getAbsolutePath()); context.setClearReferencesHttpClientKeepAliveThread(false); context.setClearReferencesStopThreads(false); context.setClearReferencesStopTimerThreads(false); context.setClearReferencesStopTimerThreads(false); context.setAntiResourceLocking(false); context.setReloadable(false); context.setUseHttpOnly(true); context.setTldValidation(false); context.setXmlValidation(false); context.setXmlNamespaceAware(false); context.setUseNaming(false); context.setDelegate(true); context.setJarScanner(new NullJarScanner()); context.setAllowCasualMultipartParsing(true); context.setCookies(false); // disable JSP and WebSocket support context.setContainerSciFilter("org.apache.tomcat.websocket.server.WsSci|org.apache.jasper.servlet.JasperInitializer"); return context; } catch (ServletException e) { throw new IllegalStateException("Fail to configure webapp from " + dir, e); } }
@Override protected void configure(ServerBuilder sb) throws Exception { // Prepare Tomcat instances. tomcatWithWebApp = new Tomcat(); tomcatWithWebApp.setPort(0); tomcatWithWebApp.setBaseDir("build" + File.separatorChar + "tomcat-" + UnmanagedTomcatServiceTest.class.getSimpleName() + "-1"); tomcatWithWebApp.addWebapp("", WebAppContainerTest.webAppRoot().getAbsolutePath()); TomcatUtil.engine(tomcatWithWebApp.getService(), "foo").setName("tomcatWithWebApp"); tomcatWithoutWebApp = new Tomcat(); tomcatWithoutWebApp.setPort(0); tomcatWithoutWebApp.setBaseDir("build" + File.separatorChar + "tomcat-" + UnmanagedTomcatServiceTest.class.getSimpleName() + "-2"); assertThat(TomcatUtil.engine(tomcatWithoutWebApp.getService(), "bar")).isNotNull(); // Start the Tomcats. tomcatWithWebApp.start(); tomcatWithoutWebApp.start(); // Bind them to the Server. sb.serviceUnder("/empty/", TomcatService.forConnector("someHost", new Connector())) .serviceUnder("/some-webapp-nohostname/", TomcatService.forConnector(tomcatWithWebApp.getConnector())) .serviceUnder("/no-webapp/", TomcatService.forTomcat(tomcatWithoutWebApp)) .serviceUnder("/some-webapp/", TomcatService.forTomcat(tomcatWithWebApp)); } };
/** * @see #addWebapp(String, String) */ public Context addWebapp(Host host, String url, String path) { return addWebapp(host, url, url, path); }
/** * Add a webapp using normal WEB-INF/web.xml if found. * * @param contextPath * @param baseDir * @return new Context * @throws ServletException */ public Context addWebapp(String contextPath, String baseDir) throws ServletException { return addWebapp(getHost(), contextPath, baseDir); }
/** * Add a webapp using normal WEB-INF/web.xml if found. * * @param contextPath * @param baseDir * @return new Context * @throws ServletException */ public Context addWebapp(String contextPath, String baseDir) throws ServletException { return addWebapp(getHost(), contextPath, baseDir); }
/** * Add a webapp using normal WEB-INF/web.xml if found. * * @param contextPath * @param baseDir * @return new Context * @throws ServletException */ public Context addWebapp(String contextPath, String baseDir) throws ServletException { return addWebapp(getHost(), contextPath, baseDir); }
/** * Add a webapp using normal WEB-INF/web.xml if found. * * @param contextPath * @param baseDir * @return new Context * @throws ServletException */ public Context addWebapp(String contextPath, String baseDir) throws ServletException { return addWebapp(getHost(), contextPath, baseDir); }
/** * Add a webapp using normal WEB-INF/web.xml if found. * * @param contextPath * @param baseDir * @return new Context * @throws ServletException */ public Context addWebapp(String contextPath, String baseDir) throws ServletException { return addWebapp(getHost(), contextPath, baseDir); }
protected void doSetupWebappContextWebappDir() throws ServletException { final String webappPath = prepareWebappPath(); final String docBase = new File(webappPath).getAbsolutePath(); final Context context = server.addWebapp(contextPath, docBase); final String webXmlPath = prepareWebXmlPath(webappPath); context.getServletContext().setAttribute(Globals.ALT_DD_ATTR, webXmlPath); }
protected void doSetupWebappContextWar(String warPath) throws ServletException { server.addWebapp(contextPath, warPath); if (!isUnpackWARsDisabled()) { prepareUnpackWARsEnv(); } }
tomcat.addWebapp(tomcat.getHost(), contextPath, webAppPath);
public class MyServer { public static void main(String[] args) throws Exception { Tomcat tomcat = new Tomcat(); tomcat.setPort(8080); tomcat.addWebapp("","webapp"); tomcat.start(); tomcat.getServer().await(); } }
public TomcatWebServer(String appBase, int port, String bindInterface) throws ServletException { super(checkNotNull(appBase), port, bindInterface, "Apache Tomcat", "org.apache.tomcat", "tomcat-catalina"); tomcat = new Tomcat(); tomcat.setPort(port); tomcat.setBaseDir("."); tomcat.getHost().setAppBase("."); String contextPath = "/"; // Add AprLifecycleListener StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); server.addLifecycleListener(listener); context = tomcat.addWebapp(contextPath, appBase); }