Accueil > java > Handling static resources with Jetty

Handling static resources with Jetty

Here is a quick (and easy) tip on how to handle static resources with Jetty6 (this is really nice and quick to setup such things for a demo ;o)). Here is the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package foo.bar;
 
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.Context;
 
/**
 * @author Christophe HAMERLING - eBM WebSourcing
 *
 */
public class JettyServer {
 
    public static void main(String[] args) throws Exception {
        Server server = new Server();
        final ContextHandlerCollection contexts = new ContextHandlerCollection();
        final Context context = new Context(contexts, "/", Context.SESSIONS);
        context.setResourceBase(System.getProperty("user.home"));
        context.addServlet("org.mortbay.jetty.servlet.DefaultServlet", "/");
 
        final SelectChannelConnector nioConnector = new SelectChannelConnector();
        nioConnector.setPort(1978);
        server.addConnector(nioConnector);
        server.setHandler(contexts);
        server.start();
    }
}

This will display your home directory on http://localhost:1978

Cheers

Share and Enjoy:
  • Digg
  • Google Bookmarks
  • del.icio.us
  • Facebook
  • StumbleUpon
  • email
Categories: java Tags: ,
  1. Pas encore de commentaire
  1. Pas encore de trackbacks