Monday, May 18, 2009

Run servlet using Jetty

Jetty is embedded server, unlike tomcat it is simple yet powerful to host even in your production site. Jetty is also perfect to run you servlet in your local computer during development, and makes debugging webapp easier.

In main class import these classes

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;


In main method create Server and Context class then add servlet classes to context instance.

Server server = new Server(8080);
Context root;
root = new Context(server, "/mycontext", Context.SESSIONS);
root.addServlet(MyServlet.class, "/url-map");
server.start();


Open browser and enter url http://localhost:8080/mycontext/url-map