Difference between revisions of "Jetty/Tutorial/Embedding Jetty"

From Eclipsepedia

Jump to: navigation, search
Line 20: Line 20:
 
public class SimplestServer
 
public class SimplestServer
 
{
 
{
     public static void main(String[] args) throws Exception
+
     public static void main(String[] args) throws Exception
 
     {
 
     {
 
         Server server = new Server(8080);
 
         Server server = new Server(8080);
Line 30: Line 30:
  
 
This runs a HTTP server on port 8080.  It is not a very useful server as it has not handlers and thus will return a 404 error for every request.
 
This runs a HTTP server on port 8080.  It is not a very useful server as it has not handlers and thus will return a 404 error for every request.
 
== Connectors ==
 
  
 
== Handlers ==
 
== Handlers ==
 +
In order to produce a response to a request, Jetty requires a [http://download.eclipse.org/jetty/stable-7/xref/org/eclipse/jetty/xml/XmlParser.html Handler] to be set on the server.  A handler may:
 +
* examine/modify the HTTP request.
 +
* generate the complete HTTP response.
 +
* call another Handler (see [http://download.eclipse.org/jetty/stable-7/xref/org/eclipse/jetty/server/handler/HandlerWrapper.html HandlerWrapper])
 +
* Select one or many Handlers to call (see [ http://download.eclipse.org/jetty/stable-7/xref/org/eclipse/jetty/server/handler/HandlerCollection.html HandlerCollection]
 +
 +
Complex request handling is typically built from multiple Handlers. Some of the handlers available in Jetty can be seen in the [http://download.eclipse.org/jetty/stable-7/xref/org/eclipse/jetty/server/handler/package-frame.html org.eclipse.jetty.server.handler] package.
 +
 +
=== Hello World Handler ===
 +
The following code from [http://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk/example-jetty-embedded/src/main/java/org/eclipse/jetty/embedded/HelloHandler.java HelloHandler.java] shows a simple hello world handler:
 +
<pre>public class HelloHandler extends AbstractHandler
 +
{
 +
    final String _greeting;
 +
    final String _body;
 +
   
 +
    public HelloHandler()
 +
    {
 +
        _greeting="Hello World";
 +
        _body=null;
 +
    }
 +
   
 +
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
 +
    {
 +
        response.setContentType("text/html");
 +
        response.setStatus(HttpServletResponse.SC_OK);
 +
        baseRequest.setHandled(true);
 +
       
 +
        response.getWriter().println("<h1>"+_greeting+"</h1>");
 +
        response.getWriter().println("from " + ((Request)request).getConnection().getConnector().getName());
 +
        response.getWriter().println("<br/>");
 +
        if (_body!=null)
 +
            response.getWriter().println(_body);
 +
    }
 +
}
 +
</pre>
 +
 +
The parameters passed to the handle method are:
 +
;target:The target of the request which is either a URI or a name from a named dispatcher.
 +
;baseRequest:The Jetty mutable request object, which is always unwrapped.
 +
;request:The immutable request object, which may have been wrapped.
 +
;response:The response that may have been wrapped.
 +
 +
 +
 +
 +
 +
 +
== Connectors ==
  
 
== Handler Collections ==
 
== Handler Collections ==
Line 39: Line 85:
 
== File Server ==
 
== File Server ==
  
 +
== Servlets ==
  
 +
== Context ==
  
 +
== ServletContext ==
  
== Context
+
== WebApplicationContext ==
  
 
}}
 
}}

Revision as of 04:33, 27 July 2009



Contents

Introduction

Jetty has a slogan "Don't deploy your application in Jetty, deploy Jetty in your application". What this means is that Jetty as an alternative to bundling your application as a standard WAR to be deployed in Jetty, Jetty is designed to be a software component that can be instantiated and used in a java program just like any POJO.

This tutorial takes you step by step from the simplest jetty server instantiation, through programmatically, to running multiple web applications with standards based deployment descriptors.

The source for most of these examples is part of the standard jetty project.

Details

To embed a Jetty server, the following steps are typical:

  1. Create the server
  2. Add/Configure Connectors
  3. Add/Configure Handlers
  4. Add/Configure Servlets/Webapps to Handlers
  5. start the server
  6. wait (join the server to prevent main exiting).

Servers

The following code from SimplestServer.java will instantiate and run the simplest possible Jetty server:

public class SimplestServer
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);
        server.start();
        server.join();
    }
}

This runs a HTTP server on port 8080. It is not a very useful server as it has not handlers and thus will return a 404 error for every request.

Handlers

In order to produce a response to a request, Jetty requires a Handler to be set on the server. A handler may:

Complex request handling is typically built from multiple Handlers. Some of the handlers available in Jetty can be seen in the org.eclipse.jetty.server.handler package.

Hello World Handler

The following code from HelloHandler.java shows a simple hello world handler:

public class HelloHandler extends AbstractHandler
{
    final String _greeting;
    final String _body;
    
    public HelloHandler()
    {
        _greeting="Hello World";
        _body=null;
    }
    
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        
        response.getWriter().println("<h1>"+_greeting+"</h1>");
        response.getWriter().println("from " + ((Request)request).getConnection().getConnector().getName());
        response.getWriter().println("<br/>");
        if (_body!=null)
            response.getWriter().println(_body);
    }
}

The parameters passed to the handle method are:

target
The target of the request which is either a URI or a name from a named dispatcher.
baseRequest
The Jetty mutable request object, which is always unwrapped.
request
The immutable request object, which may have been wrapped.
response
The response that may have been wrapped.




Connectors

Handler Collections

File Server

Servlets

Context

ServletContext

WebApplicationContext