TempDirInitializer implements ServletContainerInitializer { @Override public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException { File baseDir = new File("tmp"); String name = servletContext.getContextPath(); name = name.replaceAll("/", "_"); if (name.trim().equals("")) { name = "ROOT"; } File tempDir = new File(baseDir, name); if (!tempDir.exists()) { tempDir.mkdirs(); } LOGGER.log(FINE, "Setting TEMPDIR for context ''{0}'' to ''{1}''", new Object[]{servletContext.getContextPath(), tempDir}); servletContext.setAttribute(TEMPDIR, tempDir); }  @Override void onStartup(Set<Class<?>> classes, ServletContext servletContext);  }
@Test void testOnStartup() throws Exception { DefaultWebApplication webApplication = new DefaultWebApplication(); webApplication.setContextPath("my_servlet_context_name"); TempDirInitializer initializer = new TempDirInitializer(); initializer.onStartup(null, webApplication); File tempDir = new File("tmp/my_servlet_context_name"); assertTrue(tempDir.exists()); tempDir.delete(); tempDir.getParentFile().delete(); }