Jetty web application support library providing comprehensive webapp deployment, configuration, and class loading capabilities
—
WebAppContext is the central component for managing web application deployment in Jetty. It orchestrates the complete lifecycle of a web application, from initial configuration through deployment and runtime management.
The main entry point for creating and managing web application contexts.
/**
* Web application context that coordinates construction and configuration of
* nested handlers including security, session, and servlet handlers.
*/
public class WebAppContext extends ServletContextHandler implements WebAppClassLoader.Context {
// Core constructors
public WebAppContext();
public WebAppContext(String webApp, String contextPath);
public WebAppContext(Resource webApp, String contextPath);
public WebAppContext(HandlerContainer parent, String webApp, String contextPath);
public WebAppContext(HandlerContainer parent, Resource webApp, String contextPath);
// Full constructor for custom handler configuration
public WebAppContext(HandlerContainer parent, String contextPath,
SessionHandler sessionHandler, SecurityHandler securityHandler,
ServletHandler servletHandler, ErrorHandler errorHandler, int options);
}Usage Examples:
// Basic webapp setup
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/myapp");
webapp.setWar("myapp.war");
// With parent container
WebAppContext webapp = new WebAppContext(server, "myapp.war", "/myapp");
// With custom handlers
WebAppContext webapp = new WebAppContext(
server, "/myapp",
new SessionHandler(),
new ConstraintSecurityHandler(),
new ServletHandler(),
new ErrorPageErrorHandler(),
ServletContextHandler.SESSIONS | ServletContextHandler.SECURITY
);Methods for managing the pluggable configuration system.
/**
* Get the configuration class names
*/
public String[] getConfigurationClasses();
/**
* Get the configurations object managing all configuration instances
*/
public Configurations getConfigurations();
/**
* Set configuration classes by name
*/
public void setConfigurationClasses(String[] configurations);
public void setConfigurationClasses(List<String> configurations);
/**
* Set configuration instances directly
*/
public void setConfigurations(Configuration[] configurations);
/**
* Add configurations to the existing set
*/
public void addConfiguration(Configuration... configuration);
/**
* Get configuration by type
*/
public <T> T getConfiguration(Class<? extends T> configClass);
/**
* Remove configurations
*/
public void removeConfiguration(Configuration... configurations);
public void removeConfiguration(Class<? extends Configuration>... configurations);Usage Examples:
// Set standard configurations
webapp.setConfigurationClasses(new String[] {
"org.eclipse.jetty.webapp.WebInfConfiguration",
"org.eclipse.jetty.webapp.WebXmlConfiguration",
"org.eclipse.jetty.webapp.MetaInfConfiguration"
});
// Add custom configuration
webapp.addConfiguration(new MyCustomConfiguration());
// Get specific configuration
WebXmlConfiguration webXml = webapp.getConfiguration(WebXmlConfiguration.class);Methods for specifying and managing webapp resources.
/**
* Set the web application WAR file or directory
*/
public void setWar(String war);
public void setWarResource(Resource war);
public String getWar();
/**
* Get the WEB-INF directory resource
*/
public Resource getWebInf();
/**
* Manage resource aliases for virtual paths
*/
public Map<String, String> getResourceAliases();
public void setResourceAliases(Map<String, String> map);
public void setResourceAlias(String alias, String uri);
public String getResourceAlias(String path);
public String removeResourceAlias(String alias);
/**
* Manage extra classpath resources
*/
public List<Resource> getExtraClasspath();
public void setExtraClasspath(String extraClasspath);
public void setExtraClasspath(List<Resource> extraClasspath);Usage Examples:
// Set WAR file location
webapp.setWar("path/to/myapp.war");
// Add extra classpath entries
webapp.setExtraClasspath("lib/extra.jar:lib/configs");
// Set resource aliases
webapp.setResourceAlias("/images", "/static/img");Methods for managing XML descriptors that configure the webapp.
/**
* Get/set the defaults descriptor (web-defaults.xml)
*/
public String getDefaultsDescriptor();
public void setDefaultsDescriptor(String defaultsDescriptor);
/**
* Get/set the main web descriptor (web.xml)
*/
public String getDescriptor();
public void setDescriptor(String descriptor);
/**
* Manage override descriptors
*/
public String getOverrideDescriptor();
public List<String> getOverrideDescriptors();
public void setOverrideDescriptor(String overrideDescriptor);
public void setOverrideDescriptors(List<String> overrideDescriptors);
public void addOverrideDescriptor(String overrideDescriptor);Usage Examples:
// Use custom web-defaults.xml
webapp.setDefaultsDescriptor("config/custom-webdefaults.xml");
// Add override descriptors
webapp.addOverrideDescriptor("config/production-overrides.xml");
webapp.addOverrideDescriptor("config/database-overrides.xml");Methods for configuring the webapp's class loading behavior.
/**
* Get/set system class patterns (loaded by system classloader)
*/
public ClassMatcher getSystemClassMatcher();
public void setSystemClassMatcher(ClassMatcher systemClasses);
public void addSystemClassMatcher(ClassMatcher systemClasses);
public String[] getSystemClasses();
/**
* Get/set server class patterns (loaded by server classloader)
*/
public ClassMatcher getServerClassMatcher();
public void setServerClassMatcher(ClassMatcher serverClasses);
public void addServerClassMatcher(ClassMatcher serverClasses);
public String[] getServerClasses();
/**
* Control parent-first vs child-first class loading
*/
public boolean isParentLoaderPriority();
public void setParentLoaderPriority(boolean java2compliant);Usage Examples:
// Add system classes (loaded by system classloader)
webapp.addSystemClassMatcher(new ClassMatcher("com.mycompany.shared."));
// Add server classes (hidden from webapp)
webapp.addServerClassMatcher(new ClassMatcher("org.eclipse.jetty."));
// Use parent-first class loading
webapp.setParentLoaderPriority(true);Methods for managing the webapp deployment lifecycle.
/**
* Pre-configure the webapp (setup classloader, parse descriptors)
*/
public void preConfigure() throws Exception;
/**
* Configure the webapp (process configurations)
*/
public boolean configure() throws Exception;
/**
* Post-configure the webapp (finalize setup)
*/
public void postConfigure() throws Exception;
/**
* Get any exception that prevented startup
*/
public Throwable getUnavailableException();Usage Examples:
try {
webapp.preConfigure();
if (webapp.configure()) {
webapp.postConfigure();
webapp.start();
}
} catch (Exception e) {
Throwable cause = webapp.getUnavailableException();
// Handle startup failure
}Various configuration properties that control webapp behavior.
/**
* Distributable webapp configuration
*/
public boolean isDistributable();
public void setDistributable(boolean distributable);
/**
* WAR extraction configuration
*/
public boolean isExtractWAR();
public void setExtractWAR(boolean extractWAR);
/**
* Directory copying configuration
*/
public boolean isCopyWebDir();
public void setCopyWebDir(boolean copy);
public boolean isCopyWebInf();
public void setCopyWebInf(boolean copyWebInf);
/**
* Configuration discovery
*/
public boolean isConfigurationDiscovered();
public void setConfigurationDiscovered(boolean discovered);
/**
* Fragment handling
*/
public boolean isAllowDuplicateFragmentNames();
public void setAllowDuplicateFragmentNames(boolean allowDuplicateFragmentNames);
/**
* Exception handling
*/
public boolean isThrowUnavailableOnStartupException();
public void setThrowUnavailableOnStartupException(boolean throwIfStartupException);
/**
* Logging configuration
*/
public boolean isLogUrlOnStart();
public void setLogUrlOnStart(boolean logOnStart);Methods for managing the webapp's temporary directory.
/**
* Get/set the temporary directory for the webapp
*/
public File getTempDirectory();
public void setTempDirectory(File dir);
/**
* Control whether temp directory persists after webapp stops
*/
public boolean isPersistTempDirectory();
public void setPersistTempDirectory(boolean persist);Methods for managing webapp security and permissions.
/**
* Get/set permissions for the webapp
*/
public PermissionCollection getPermissions();
public void setPermissions(PermissionCollection permissions);
/**
* Set context whitelist for cross-context access
*/
public void setContextWhiteList(String... contextWhiteList);Static utility methods for webapp management.
/**
* Get the current WebAppContext from the current thread
*/
public static WebAppContext getCurrentWebAppContext();
/**
* Add server class patterns to all webapps on a server
*/
public static void addServerClasses(Server server, String... pattern);
/**
* Add system class patterns to all webapps on a server
*/
public static void addSystemClasses(Server server, String... pattern);Usage Examples:
// Get current webapp context (useful in servlets/filters)
WebAppContext currentContext = WebAppContext.getCurrentWebAppContext();
// Add server-wide class patterns
WebAppContext.addServerClasses(server, "com.mycompany.server.");
WebAppContext.addSystemClasses(server, "com.mycompany.shared.");Access to the webapp's configuration metadata.
/**
* Get the metadata object containing all webapp configuration information
*/
public MetaData getMetaData();The inner Context class that extends ServletContextHandler.Context with webapp-specific behavior.
/**
* WebApp-specific servlet context implementation
*/
public static class Context extends ServletContextHandler.Context {
/**
* Check if a listener class type is valid for this webapp
*/
public void checkListener(Class<? extends EventListener> listener) throws IllegalStateException;
/**
* Get resource URL with webapp-specific resource resolution
*/
public URL getResource(String path) throws MalformedURLException;
/**
* Get servlet context for a URI path (cross-context access)
*/
public ServletContext getContext(String uripath);
}// Standard servlet context attributes
public static final String TEMPDIR = ServletContext.TEMPDIR;
public static final String BASETEMPDIR = "org.eclipse.jetty.webapp.basetempdir";
// Default configuration files
public static final String WEB_DEFAULTS_XML = "org/eclipse/jetty/webapp/webdefault.xml";
// Server configuration attributes
public static final String SERVER_SYS_CLASSES = "org.eclipse.jetty.webapp.systemClasses";
public static final String SERVER_SRV_CLASSES = "org.eclipse.jetty.webapp.serverClasses";
// Error handling
public static final String ERROR_PAGE = "org.eclipse.jetty.server.error_page";
// Default class matchers
public static final ClassMatcher __dftSystemClasses;
public static final ClassMatcher __dftServerClasses;Install with Tessl CLI
npx tessl i tessl/maven-org-eclipse-jetty--jetty-webapp