Writing a Jetty Handler
The org.mortbay.jetty.Handler interface provides Jetty's core of content generation or manipulation. Classes that implement this interface are used to coordinate requests, filter requests and generate content.
The core API of the Handler interface is
The target of a handler is an identifier for the resource that should handle the passed request. This is normally the URI that is parsed from a HTTP Request. However, in two key circumstances the target may differ from the URI of the passed request:
- If the request has been dispatched to a named resource, such as a named servlet, then the target is the name of that resource.
- If the request is being made by a call to RequestDispatcher.include(...)
then the target is the URI of the included resource and will be different to the URI of the actual request.
The Request and Response
The request and response objects used in the signature of the handle method are HttpServletRequest and HttpServletResponse. These are the standard APIs and are moderately restricted in what can be done to the request and response. More often than not, access to the jetty implementations of these classes is required: Request and Response. However, as the request and response may be wrapped by handlers, filters and servlets, it is not possible to pass the implementation directly. The following mantra will retrieve the core implementation objects from under any wrappers:
Handlers are usually arranged in a list and a request presented to each handler in turn until (or at most) one indicates that the request has been handled. In this situation, this will allow handlers to:
- Ignore requests that are not applicable
- Handle requests by populating the response and/or generating content
- Modify the request but allow it to pass onto the next handler(s). Headers and attributes may be modified or an InputStream filter added
- Modify the response but allow the request to pass onto the next handler(s). Headers may be modified or OutputStream filters added
These are the handlers that implement the org.mortbay.jetty.Handler interface package:
There are some examples of implementing custom handlers in the Embedded Examples.
See also Newbie Guide to Jetty.