Jetty has moved!
Jetty is a project at the Eclipse Foundation.
Homepage:http://www.eclipse.org/jetty
Downloads: http://download.eclipse.org/jetty/
Documentation:http://www.eclipse.org/jetty/documentation/current/
About:http://www.eclipse.org/jetty/about.php
Jetty Powered:http://www.eclipse.org/jetty/powered/
Contact the core Jetty developers at www.webtide.com
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
Skip to end of metadata
Go to start of metadata

Embedding Jetty

Quick Start

Quick Start - Handlers

The following code implements an anonymous hello handler and starts a jetty server with it:

Quick Start - Servlets

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

Quick Start - jetty.xml

You can programatically create a jetty Server, and still use a jetty.xml file to configure it:

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:

More Examples

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

  • FileServer - simple HTTP file server
  • OneHandler - Embed a single handler - useful when there is only a single simple source of content.
  • ManyHandlers - Embed multiple handlers, one called after the other on each request.
  • OneContext - An example of a handler within a context, which allows a contextPath, resourceBase and class loader to be set.
  • MinimalServlets - An example of a ServletHandler without a context.
  • OneServletContext - A servletHandler within a context, which has been constructed with a session handler.
  • ManyServletContexts - Multiple servlet contexts using context path to select the context.
  • LikeJettyXml - a code example that mimics the configuration of the standard jetty.xml file
  • FromXmlConfiguration - a example that shows how XML fragments may be used to build a server and/or context.
  • OneWebApp - an example showing a single webapp being added programmatically.

Setting up the classpath

To run Jetty you need only the following jars on the classpath:

  • servlet-api-2.5-6.x.jar
  • jetty-util-6.x.jar
  • jetty-6.x.jar

This gives you the capability to handle HTTP with handlers, servlets and webapplications.
Additional features such as JSP and AJP require additional jars (normally found in subdirectories of $JETTY_HOME/lib).

Java Server Pages

JSP2.1

Remember!

Icon

You need to use J2SE5 (aka jdk 1.5) if you wish to use JSP2.1.

This is the jsp version mandated by servlet specification 2.5. You will need these jars:

  • ant-1.6.5.jar
  • core-3.1.1.jar
  • jsp-2.1.jar
  • jsp-api-2.1.jar

JSP2.0

JSP 2.0 is used with version 2.4 of the servlet specification. Depending on the functionality you require, you may be able to use this version rather than the newer JSP2.1
These are the dependencies when JSP 2.0 is used:

  • ant-1.6.4.jar
  • jasper-compiler-5.5.15.jar
  • jasper-runtime-5.5.15.jar
  • jsp-api-2.0.jar
  • commons-el-1.0.jar
  • jcl104-over-slf4j-1.0-rc5.jar
  • slf4j-simple-1.0-rc5.jar
  • xmlParserAPIs-2.6.2.jar
  • xercesImpl-2.6.2.jar
  • No labels

14 Comments

  1. I have written an open-source application that uses Jetty6 embedded:
     http://www.source-code.biz/ServletViewer/
    Maybe this could be useful for others to get a start.

  2. As long there is not tutorial you might want to have a look at: Quick Guide to Embedding Jetty

  3. In the jetty.xml example, the argument for the XmlConfiguration constructor should be: new FileInputStream("myJetty.xml").

  4. Clinton,

    Thanks for pointing this out. Fixed now.

    Jan

  5. The example for MinimalServlets has an error.  It never calls:
    handler.initialize();
    HTH,
    Tim

  6. Tim, fixed in svn trunk. BTW the fix was in the ServletHandler class to ensure that the ServletHolders get initialized in the absence of any associated Context. Thanks for noticing this.

  7. I have ported my application from Jetty5 to Jetty6 and while doing so I "discovered" a few things that I wish to share with all of you.

    You can find the log and comments of it at the following

    http://www.engidea.com/blog/informatica/jetty6/jetty6-explored.html

    I hope you find it useful.

  8. For my test rig, I found the examples provided in the links a bit confusing. I have an expanded, single webapp that I need to test. It has a web.xml that has specifications to set up Spring MVC, etc.

    The good news is that, from the comments above, I found Christian's source very brief and understandable.  For my single-context test rig, I simplified even more:

    Consider adding an example which just does this most simple invocation.

     larry

  9. I want to use Hessian protocol http://www.caucho.com/hessian/ between Server and Client. For server i need servlet container (Tomcat, Jetty...). I choose Jetty and write next code to realize Hessian Server into Jetty Embeded servlet container:


    But i need to to pass 2 parameters for com.caucho.hessian.server.HessianServlet Servlet - this parameter i to pass over simple web.xml in simple deploy servlet:


    How i can to pass this parameter for servlet over usual code (for example in my code in higher)???

  10. For use of Jetty as an embedded container, where the full power of Servlets is not needed, ContextHandler is an extremely useful lightweight class. In particular it allows defining custom error handlers for the application.

  11. Running Apache Axis in Embedded Jetty


    Also server-config.wsdd has to be in the classpath, i put it in the top level of my src jar. 

  12. We've been using Jetty in an embedded application for over five years, but upon upgrading to 6.1.x it is now running in a separate JVM from the parent application, which is causing all sorts of headaches (e.g., we can't pass our own objects across the JVM boundary). Is there a way to force use of the same JVM as the parent app? We're currently using Jetty 6.1.5 with Java 5.

    Thanks for any help! -- Ichiro Furusato

  13. Hi

    Is it possible to add servlets to a context when the server is running ?

    Thanks 

  14. Can someone help me put together an example of creating a URL rewriting handler in embedded mode? I need to have a URL like:

    http://localhost:8080/groups/1234567

    rewrite to

    http://localhost:8080/groups/group?id=1234567

    The "pattern" argument confuses me because it seems to be proprietary to Jetty vs. being based on regular expressions. I couldn't find documentation on this anywhere, and there are not many comments in the code.

Contact the core Jetty developers at www.webtide.com
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