Jetty Server Tutorial      Back Index Exit Next

The org.mortbay.jetty.Server class is an subclass of org.mortbay.http.HttpServer that adds conveniance methods for configuring the servlet container provided by the org.mortbay.jetty.servlet package.

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: Jetty also has a web application extension for jetty specific configuration. If a file called "web-jetty.xml" is found in the WEB-INF directory, it is assumes to be a Jetty XmlConfigurarion file as is applied to the WebApplication instance. Back Index Exit Next