September 21, 2010

How to embed Jetty Server

« Track window and widget events with AT-SPI | Main | AutoFS - Auto-Mount of Hard Drives »

The Jetty J2EE webcontainer represents a highly modularized server application. Because of that you are able to start the web server directly in Java code. For example that allows developers to deploy Servlets, JSP pages and WAR files within JUnit tests or use the webcontainer directly for other issues. The following example shows basically how to start and configure Jetty.



package org.developers.blog.jetty.embedded;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;

public class JettyExample {

    public static void main(String[] args) throws Exception {
        Server server = new Server();

        Connector connector = new SelectChannelConnector();
        connector.setPort(8080);
        connector.setHost("127.0.0.1");
        server.addConnector(connector);

        WebAppContext wac = new WebAppContext();
        wac.setContextPath("/");
        //expanded war or path of war file
        wac.setWar("./src/main/resources/web");
        server.addHandler(wac);
        server.setStopAtShutdown(true);

        //another way is to use an external jetty configuration file
        //XmlConfiguration configuration =
        //new XmlConfiguration(new File("/path/jetty-config.xml").toURL());
        //configuration.configure(server);

        server.start();
    }
}

Best regards
Rafael Sobek

Technorati Tags:

Posted by rafael.sobek at 10:06 PM in Java

 

[Trackback URL for this entry]

Pingback: Blog bookmarks 09/24/2010 « My Diigo bookmarks at Fr, 24 Sep 4:31 AM

How to embedd Jetty Server
groovy How to embedd Jetty

Comment: Shantanu Kumar at So, 26 Sep 12:52 PM

Maybe use Jettify instead? it's dead easy to use and supports servlets, JSP etc.

Jettify: http://code.google.com/p/bitumenframework/

Comment: Christian Harms at So, 26 Sep 7:14 PM

For unittest yiu have to sure port 8080 is free!

Comment: Benjamin Keil at Mo, 27 Sep 9:37 AM

If you also want to be able to stop the embedded Jetty server, check out this post: http://ptrthomas.wordpress.com/2009/01/24/how-to-start-and-stop-jetty-revisited/

Pingback: Revue de presse, semaine 39 - Espace de fouille at Mo, 4 Okt 8:23 AM

How to embed Jetty Server
Play TDD #26: Integrating StockMarket into StockMarketTableModel Dive into HTML 5 How to embed Jetty Server A

Your comment:

(not displayed)
 
 
 

Live Comment Preview: