Jetty web application support library providing comprehensive webapp deployment, configuration, and class loading capabilities
—
Jetty WebApp provides specialized classloaders that implement servlet specification compliant class loading with advanced visibility controls, caching capabilities, and support for class transformation.
Servlet specification compliant classloader with fine-grained class visibility controls.
/**
* Specialized classloader for web applications with servlet spec compliant
* loading behavior and class visibility controls.
*/
public class WebAppClassLoader extends URLClassLoader implements ClassVisibilityChecker {
/**
* Create classloader with context configuration
*/
public WebAppClassLoader(Context context) throws IOException;
/**
* Create classloader with parent and context
*/
public WebAppClassLoader(ClassLoader parent, Context context) throws IOException;
/**
* Get/set the classloader name for debugging
*/
public String getName();
public void setName(String name);
/**
* Get the configuration context
*/
public Context getContext();
}Usage Examples:
// Create with webapp context
WebAppClassLoader loader = new WebAppClassLoader(webAppContext);
loader.setName("MyWebApp ClassLoader");
// Create with custom parent
WebAppClassLoader loader = new WebAppClassLoader(
Thread.currentThread().getContextClassLoader(),
webAppContext
);Methods for managing the classloader's classpath.
/**
* Add a resource to the classpath
*/
public void addClassPath(Resource resource) throws IOException;
/**
* Add classpath entries from string (semicolon/colon separated)
*/
public void addClassPath(String classPath) throws IOException;
/**
* Add all JAR files from a directory to the classpath
*/
public void addJars(Resource lib) throws IOException;Usage Examples:
// Add individual JAR
loader.addClassPath(Resource.newResource("lib/mylib.jar"));
// Add classpath string
loader.addClassPath("lib/jar1.jar:lib/jar2.jar:classes");
// Add all JARs from directory
loader.addJars(Resource.newResource("WEB-INF/lib"));Methods for controlling class visibility and delegation behavior.
/**
* Check if a class should be loaded by the system classloader
*/
public boolean isSystemClass(Class<?> clazz);
/**
* Check if a class should be loaded by the server classloader
*/
public boolean isServerClass(Class<?> clazz);Usage Examples:
// Check class visibility
if (loader.isSystemClass(MyClass.class)) {
// Class will be loaded by system classloader
}
if (loader.isServerClass(SomeJettyClass.class)) {
// Class will be loaded by server classloader (hidden from webapp)
}Support for bytecode transformation during class loading.
/**
* Add a class file transformer for bytecode modification
*/
public void addTransformer(ClassFileTransformer transformer);
/**
* Remove a class file transformer
*/
public boolean removeTransformer(ClassFileTransformer transformer);Usage Examples:
// Add instrumentation transformer
ClassFileTransformer transformer = new MyInstrumentationTransformer();
loader.addTransformer(transformer);
// Remove transformer
loader.removeTransformer(transformer);Methods for executing code with specific class loading privileges.
/**
* Run action with server class access privileges
*/
public static <T> T runWithServerClassAccess(PrivilegedExceptionAction<T> action)
throws PrivilegedActionException;Usage Examples:
// Execute with server class access
String result = WebAppClassLoader.runWithServerClassAccess(() -> {
// This code can access server classes normally hidden from webapp
return ServerUtility.getInternalInfo();
});Methods for managing classloader lifecycle.
/**
* Close the classloader and release all resources
*/
public void close() throws IOException;Performance-optimized classloader with resource and class caching for improved performance.
/**
* WebAppClassLoader that caches resource lookups and class loading
* for improved performance in development and production environments.
* Implements JMX management for cache control.
*/
@ManagedObject("Caching WebApp ClassLoader")
public class CachingWebAppClassLoader extends WebAppClassLoader {
/**
* Create caching classloader with context
*/
public CachingWebAppClassLoader(Context context) throws IOException;
/**
* Create caching classloader with parent and context
*/
public CachingWebAppClassLoader(ClassLoader parent, Context context) throws IOException;
/**
* Create caching classloader with all parameters
*/
public CachingWebAppClassLoader(ClassLoader parent, Context context,
ClassLoader parentPriorityClassLoader) throws IOException;
/**
* Clear all cached resources and classes (JMX managed operation)
*/
@ManagedOperation(value = "Clear cache", impact = "ACTION")
public void clearCache();
/**
* Get the number of cached classes
*/
@ManagedAttribute("Cached Class Count")
public int getCachedClassCount();
/**
* Get the number of cached resources
*/
@ManagedAttribute("Cached Resource Count")
public int getCachedResourceCount();
/**
* Check if caching is enabled
*/
@ManagedAttribute("Caching Enabled")
public boolean isCaching();
/**
* Enable or disable caching
*/
public void setCaching(boolean cache);
}Usage Examples:
// Create caching classloader
CachingWebAppClassLoader loader = new CachingWebAppClassLoader(webAppContext);
// Clear cache when needed (e.g., during development)
loader.clearCache();Key methods overridden to provide webapp-specific behavior.
/**
* Get resource with caching (CachingWebAppClassLoader only)
*/
public URL getResource(String name);
/**
* Load class with caching (CachingWebAppClassLoader only)
*/
public Class<?> loadClass(String name) throws ClassNotFoundException;The Context interface provides configuration and resource access for classloaders.
/**
* Context interface providing classloader configuration and resource access
*/
public interface Context extends ClassVisibilityChecker {
/**
* Convert URL/path to resource
*/
Resource newResource(String urlOrPath) throws IOException;
/**
* Get permissions for the webapp
*/
PermissionCollection getPermissions();
/**
* Check if parent classloader has priority
*/
boolean isParentLoaderPriority();
/**
* Get extra classpath resources
*/
List<Resource> getExtraClasspath();
/**
* Check if resource should be loaded by server classloader
*/
boolean isServerResource(String name, URL parentUrl);
/**
* Check if resource should be loaded by system classloader
*/
boolean isSystemResource(String name, URL webappUrl);
}Base interface for class visibility checking.
/**
* Interface for checking class visibility and loading delegation
*/
public interface ClassVisibilityChecker {
/**
* Check if class should be loaded by parent classloader
*/
boolean isSystemClass(Class<?> clazz);
/**
* Check if class should be hidden from webapp
*/
boolean isServerClass(Class<?> clazz);
}// Create webapp with standard classloader
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/myapp");
webapp.setWar("myapp.war");
// Classloader is created automatically during webapp.start()
WebAppClassLoader loader = webapp.getClassLoader();// Create webapp with caching classloader
WebAppContext webapp = new WebAppContext() {
@Override
protected void startContext() throws Exception {
// Set custom classloader before starting
setClassLoader(new CachingWebAppClassLoader(this));
super.startContext();
}
};// Clear classloader cache during development
if (isDevelopmentMode()) {
WebAppClassLoader loader = webapp.getClassLoader();
if (loader instanceof CachingWebAppClassLoader) {
((CachingWebAppClassLoader) loader).clearCache();
}
}// Add instrumentation transformer
ClassFileTransformer transformer = new ClassFileTransformer() {
@Override
public byte[] transform(ClassLoader loader, String className,
Class<?> classBeingRedefined, ProtectionDomain protectionDomain,
byte[] classfileBuffer) throws IllegalClassFormatException {
if (className.startsWith("com/myapp/")) {
// Transform classes in com.myapp package
return instrumentClass(classfileBuffer);
}
return null; // No transformation
}
};
WebAppClassLoader loader = webapp.getClassLoader();
loader.addTransformer(transformer);Install with Tessl CLI
npx tessl i tessl/maven-org-eclipse-jetty--jetty-webapp