Servlet Mappings
The addContext()
methods return instances of the
org.mortbay.jetty.servlet.ServletHandlerContext class to allow servlet mappings to be simply configured:
ServletHandlerContext context = (ServletHandlerContext)server.getContext(null,"/"); context.addServlet("Dump", "/dump/*", "org.mortbay.servlet.Dump");This configuration gets (or creates) a context at / and maps the Dump servlet to all URLs starting with "/dump/*". This configuration can also be done without using the conveniance methods and is equivalent to the following:
HandlerContext context = server.getContext(null,"/"); ServletHandler handler = new ServletHandler(); handler.addServlet("Dump", "/dump/*", "org.mortbay.servlet.Dump"); context.addHandler(handler);
Dynamic Servlets
The ServletHandler can be configured to dynamically create mappings for servlets that have
their classnames within the requested URL:
ServletHandlerContext context = (ServletHandlerContext)server.getContext(null,"/context/*"); context.setClassPath("./servlets/"); context.setDynamicServletPathSpec("/servlet/*");If I request is made to the URL
/context/servlet/com.acme.HelloWorldServlet/info
and the
classloader for the context can find a class com.acme.HelloWorldServlet
, then that servlet is
loaded and a dynamic mapping made to it from /servlet/com.acme.HelloWorldServlet/*
.
Note that this configuration also sets a class path for the context. This allows the directory
./servlets/com/acme
to be searched for a HelloWorldServlet.class
file.
Servlets can also be loaded from the system classpath for the JVM, however this can represent a
security risk and thus can be disabled with the following call:
context.getServletHandler().setServeDynamicSystemServlets(false);
Web Applications
The Servlet Specification details a standard layout for web applications. If your static and dynamic content are
packages as a web applications, then a
org.mortbay.jetty.servlet.WebApplicationContext can be used to simple configure a context for the web application:
server.addWebApplication("/","./webapps/myapp/","./etc/webdefaults.xml");A web application can be configured with upto 5 arguments: