| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>Chapter 25. Handlers</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty"><link rel="up" href="jetty-dev-guide.html" title="Part IV. Jetty Development Guide"><link rel="prev" href="ant-and-jetty.html" title="Chapter 24. Ant and Jetty"><link rel="next" href="websocket-intro.html" title="Chapter 26. WebSocket Introduction"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><link rel="stylesheet" href="css/highlighter/foundation.css"><script src="js/highlight.pack.js"></script><script> |
| hljs.initHighlightingOnLoad(); |
| </script><link type="text/css" rel="stylesheet" href="css/font-awesome/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small"> |
| Version: 9.3.28.v20191105</span></td><td style="width: 50%"></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 25. Handlers</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ant-and-jetty.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a> </td><th width="60%" align="center">Part IV. Jetty Development Guide<br><a accesskey="p" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></th><td width="20%" align="right"> <a accesskey="n" href="websocket-intro.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/">Contact the core Jetty developers at |
| <span class="website">www.webtide.com</span></a></h5><p> |
| private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... |
| scalability guidance for your apps and Ajax/Comet projects ... development services for sponsored feature development |
| </p></div><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="jetty-handlers"></a>Chapter 25. Handlers</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="section"><a href="jetty-handlers.html#writing-custom-handlers">Writing Custom Handlers</a></span></dt></dl></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="writing-custom-handlers"></a>Writing Custom Handlers</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="section"><a href="jetty-handlers.html#handler-api">The Handler API</a></span></dt><dt><span class="section"><a href="jetty-handlers.html#handling-requests">Handling Requests</a></span></dt><dt><span class="section"><a href="jetty-handlers.html#more-about-handlers">More About Handlers</a></span></dt></dl></div><p>The Handler is the Jetty component that deals with received requests.</p><p>Many users of Jetty never need to write a Jetty Handler, but instead use the <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/servlet/package-summary.html" target="_top">Servlet API.</a> |
| You can reuse the existing Jetty handlers for context, security, sessions and servlets without the need for extension. |
| However, some users might have special requirements or footprint concerns that prohibit the use of the full servlet API. |
| For them implementing a Jetty handler is a straight forward way to provide dynamic web content with a minimum of fuss.</p><p>See the section on <a class="xref" href="architecture.html#basic-architecture" title="Jetty Architecture">Jetty Architecture</a> to understand more about Handlers vs. Servlets.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="handler-api"></a>The Handler API</h3></div></div></div><p>The <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/Handler.html" target="_top">Handler</a> 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.</p><p>The core API of the Handler interface is:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code>public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) |
| throws IOException, ServletException</code></pre><p>An implementation of this method can handle a request and pass the request onto another handler (or servlet), or it can modify and/or wrap the request before passing it on. |
| This gives three styles of handler:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">Coordinating Handlers - Handlers that route requests to other handlers (<code class="literal">HandlerCollection</code>, <code class="literal">ContextHandlerCollection</code>)</li><li class="listitem">Filtering Handlers - Handlers that augment a request and pass it on to other handlers (<code class="literal">HandlerWrapper</code>, <code class="literal">ContextHandler</code>, <code class="literal">SessionHandler</code>)</li><li class="listitem">Generating Handlers - Handlers that produce content (<code class="literal">ResourceHandler</code> and <code class="literal">ServletHandler</code>)</li></ul></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="target"></a>The Target</h4></div></div></div><p>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 an HTTP Request. |
| However, in two key circumstances the target may differ from the URI of the passed request:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">If the request has been dispatched to a named resource, such as a named servlet, the target is the name of that resource.</li><li class="listitem">If the request is being made by a call to <a class="link" href="http://docs.oracle.com/javaee/7/api/javax/servlet/RequestDispatcher.html" target="_top"><code class="literal">RequestDispatcher</code></a>, the target is the URI of the included resource and is different to the URI of the actual request.</li></ul></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="request-and-response"></a>The Request and Response</h4></div></div></div><p>The request and response objects used in the signature of the handle method are |
| <a class="link" href="http://docs.oracle.com/javaee/7/api/javax/servlet/ServletRequest.html" target="_top"><code class="literal">ServletRequest</code></a> and <a class="link" href="http://docs.oracle.com/javaee/7/api/javax/servlet/ServletResponse.html" target="_top"><code class="literal">ServletResponse</code></a>. |
| These are the standard APIs and are moderately restricted in what they can do to the request and response. |
| More often than not, access to the Jetty implementations of these classes is required: <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/Request.html" target="_top"><code class="literal">Request</code></a> and <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/Response.html" target="_top"><code class="literal">Response</code></a>. |
| 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 retrieves the core implementation objects from under any wrappers:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code>Request base_request = request instanceof Request ? (Request)request : HttpConnection.getCurrentConnection().getHttpChannel().getRequest(); |
| Response base_response = response instanceof Response ? (Response)response : HttpConnection.getCurrentConnection().getHttpChannel().getResponse();</code></pre><p>Notice that if the handler passes the request on to another handler, it should use the Request/Response objects passed in, and not the base objects. |
| This is to preserve any wrapping done by up stream handlers.</p></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="dispatch"></a>The Dispatch</h4></div></div></div><p>The dispatch argument indicates the state of the handling of the call and may be:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><code class="literal">REQUEST == 1</code> - An original request received from a connector.</li><li class="listitem"><code class="literal">FORWARD == 2</code> - A request being forwarded by a RequestDispatcher.</li><li class="listitem"><code class="literal">INCLUDE == 4</code> - A request being included by a RequestDispatcher.</li><li class="listitem"><code class="literal">ERROR == 8</code> - A request being forwarded to a error handler by the container.</li></ul></div><p>These mostly have significance for servlet and related handlers. |
| For example, the security handler only applies authentication and authorization to REQUEST dispatches.</p></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="handling-requests"></a>Handling Requests</h3></div></div></div><p>A Handler may handle a request by:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><a class="xref" href="jetty-handlers.html#generating-response" title="Generating a Response">Generating a Response</a></li><li class="listitem"><a class="xref" href="jetty-handlers.html#filtering-request-or-response" title="Filtering the Request and/or Response">Filtering the Request and/or Response</a></li><li class="listitem"><a class="xref" href="jetty-handlers.html#passing-request-and-response" title="Passing the Request and Response to Another Handler">Passing the Request and Response to Another Handler</a></li></ul></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="generating-response"></a>Generating a Response</h4></div></div></div><p>The <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/embedded/OneHandler.html" target="_top"><code class="literal">OneHandler</code></a> embedded example shows how a simple handler can generate a response.</p><p>You can use the standard servlet response API, which will typically set some status, content headers and then write out the content:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code> response.setContentType("text/html"); |
| response.setStatus(HttpServletResponse.SC_OK); |
| response.getWriter().println("<h1>Hello OneHandler</h1>");</code></pre><p>It is also very important that a handler indicate that it has completed handling the request and that the request should not be passed to other handlers:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code> Request base_request = (request instanceof Request) ? (Request)request:HttpConnection.getCurrentConnection().getHttpChannel().getRequest(); |
| base_request.setHandled(true);</code></pre></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="filtering-request-or-response"></a>Filtering the Request and/or Response</h4></div></div></div><p>Once the base request or response object is obtained, you can modify it. |
| Typically you would make modifications to accomplish:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">Breaking the URI into contextPath, servletPath and pathInfo components.</li><li class="listitem">Associating a resource base with a request for static content.</li><li class="listitem">Associating a session with a request.</li><li class="listitem">Associating a security principal with a request.</li><li class="listitem">Changing the URI and paths during a request dispatch forward to another resource.</li></ul></div><p>You can also update the context of the request:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">Setting the current threads context classloader.</li><li class="listitem">Setting thread locals to identify the current <code class="literal">ServletContext</code>.</li></ul></div><p>Typically Jetty passes a modified request to another handler and undoes modifications in a finally block afterwards:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code> try |
| { |
| base_request.setSession(a_session); |
| next_handler.handle(target,request,response,dispatch); |
| } |
| finally |
| { |
| base_request.setSession(old_session); |
| }</code></pre><p>The classes that implement the <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/HandlerWrapper.html" target="_top"><code class="literal">HandlerWrapper</code></a> class are typically handler filters of this style.</p></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="passing-request-and-response"></a>Passing the Request and Response to Another Handler</h4></div></div></div><p>A handler might simply inspect the request and use the target, request URI or other information to select another handler to pass the request to. |
| These handlers typically implement the <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/HandlerContainer.html" target="_top"><code class="literal">HandlerContainer</code></a> interface.</p><p>Examples include:</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/HandlerCollection.html" target="_top">Class <code class="literal">HandlerCollection</code></a> - |
| A collection of handlers, where each handler is called regardless of the state of the request. |
| This is typically used to pass a request to a <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/ContextHandlerCollection.html" target="_top"><code class="literal">ContextHandlerCollection</code>,</a> and then the <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/RequestLogHandler.html" target="_top"><code class="literal">RequestLogHandler</code>.</a></li><li class="listitem"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/HandlerList.html" target="_top"><code class="literal">HandlerList</code></a> - A list of handlers that are called in turn until the request state is set as handled.</li><li class="listitem"><a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/org/eclipse/jetty/server/handler/ContextHandlerCollection.html" target="_top"><code class="literal">ContextHandlerCollection</code></a> - A collection of Handlers, of which one is selected by best match for the context path.</li></ul></div></div></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="more-about-handlers"></a>More About Handlers</h3></div></div></div><p>See the <a class="link" href="http://www.eclipse.org/jetty/javadoc/9.3.28.v20191105/" target="_top">latest Jetty JavaDoc</a> for detailed information on each Jetty handler.</p></div></div></div><script type="text/javascript"> |
| SyntaxHighlighter.all() |
| </script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ant-and-jetty.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a> </td><td width="20%" align="center"><a accesskey="u" href="jetty-dev-guide.html"><i class="fa fa-chevron-up" aria-hidden="true"></i> Top</a></td><td width="40%" align="right"> <a accesskey="n" href="websocket-intro.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 24. Ant and Jetty </td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></td><td width="40%" align="right" valign="top"> Chapter 26. WebSocket Introduction</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout"> |
| See an error or something missing? |
| <span class="callout"><a href="http://github.com/eclipse/jetty.project">Contribute to this documentation at |
| <span class="website"><i class="fa fa-github" aria-hidden="true"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2019-11-05)</i></span></div></p></body></html> |