Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


Code Block
Server server = new Server();
XmlConfiguration configuration = new XmlConfiguration(new File("myJetty.xml").toURL()); //or use new XmlConfiguration(new FileInputStream("myJetty.xml"));

Quick Start - Servlets and .jsp pages (hosted within the same .jar as the embedder)

Here is a simple example of embedding Jetty using a Hello world servlet:

Code Block

// assumes that this directory contains .html and .jsp files
// This is just a directory within your source tree, and can be exported as part of your normal .jar
final String WEBAPPDIR = "com/xxx/yyy/webapp";

final Server server = new Server(httpServerPort);

final String CONTEXTPATH = "/admin";

// for localhost:port/admin/index.html and whatever else is in the webapp directory
final URL warUrl = this.class.getClassLoader().getResource(WEBAPPDIR);
final String warUrlString = warUrl.toExternalForm();
server.setHandler(new WebAppContext(warUrlString, CONTEXTPATH));

// for localhost:port/servlets/cust, etc.
final Context context = new Context(server, "/servlets", Context.SESSIONS);
context.addServlet(new ServletHolder(new CustomerServlet(whatever)), "/cust");
context.addServlet(new ServletHolder(new UserServlet(whatever)), "/user");


More Examples

These examples are all included as part of the standard Jetty distribution in the $JETTY_HOME/examples/embedded sub project.


Contact the core Jetty developers at
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery