Wednesday, August 15, 2012

Jetty/Tutorial/Embedding Jetty

All sources are from:http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
The only goal of this post is to help learning jetty.

To embed a Jetty server, the following steps are typical:
  • Create the server
  • Add/Configure Connectors
  • Add/Configure Handlers 
  • Add/Configure Servlets/Webapps to Handlers
  • Start the server
  • Wait (join the server to prevent main exiting) 
Creating a server:
public class SimplestServer
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server(8080);
        server.start();
        server.join();
    }
}
Wrting Handlers:
To produce a response to a request, Jetty requires a Handler to be set on the server. A handler may:
  • Call another Handler (see HandlerWrapper).
  • Examine/modify the HTTP request.
  • Generate the complete HTTP response.
  • Select one or many Handlers to call (see HandlerCollection).
a simple hello world handler:
public class HelloHandler extends AbstractHandler
{
    public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response) 
        throws IOException, ServletException
    {
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }
}
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 might have been wrapped.
  • response–the response, which might have been wrapped.
The handler sets the response status, content-type and marks the request as handled before it generates the body of the response using a writer. The following code from OneHandler.java shows how a Jetty server can use this handler:
public static void main(String[] args) throws Exception
{
    Server server = new Server(8080);
    server.setHandler(new HelloHandler());
 
    server.start();
    server.join();
}

Configure Connectors:
To configure the HTTP connectors that the server uses, you can set one or more connectors on the server. You can configure each connector with details such as interface, port, buffer sizes, timeouts, etc.
public class ManyConnectors
{
    public static void main(String[] args) throws Exception
    {
        Server server = new Server();
 
        SelectChannelConnector connector0 = new SelectChannelConnector();
        connector0.setPort(8080);
        connector0.setMaxIdleTime(30000);
        connector0.setRequestHeaderSize(8192);
 
        SelectChannelConnector connector1 = new SelectChannelConnector();
        connector1.setHost("127.0.0.1");
        connector1.setPort(8888);
        connector1.setThreadPool(new QueuedThreadPool(20));
        connector1.setName("admin");
 
        SslSelectChannelConnector ssl_connector = new SslSelectChannelConnector();
        String jetty_home = 
          System.getProperty("jetty.home","../jetty-distribution/target/distribution");
        System.setProperty("jetty.home",jetty_home);
        ssl_connector.setPort(8443);
        SslContextFactory cf = ssl_connector.getSslContextFactory();
        cf.setKeyStore(jetty_home + "/etc/keystore");
        cf.setKeyStorePassword("OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4");
        cf.setKeyManagerPassword("OBF:1u2u1wml1z7s1z7a1wnl1u2g");
 
        server.setConnectors(new Connector[]{ connector0, connector1, ssl_connector });
 
        server.setHandler(new HelloHandler());
 
        server.start();
        server.join();
    }
}




























No comments:

Post a Comment