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
Embedding Jetty
Quick Start
Quick Start - Handlers
The following code implements an anonymous hello handler and starts a jetty server with it:
Handler handler=new AbstractHandler() { public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch) throws IOException, ServletException { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>Hello</h1>"); ((Request)request).setHandled(true); } }; Server server = new Server(8080); server.setHandler(handler); server.start();
Quick Start - Servlets
Here is a simple example of embedding Jetty using a Hello world servlet:
Server server = new Server(8080); Context root = new Context(server,"/",Context.SESSIONS); root.addServlet(new ServletHolder(new HelloServlet("Ciao")), "/*"); server.start();
Quick Start - jetty.xml
You can programatically create a jetty Server, and still use a jetty.xml file to configure it:
Server server = new Server(); XmlConfiguration configuration = new XmlConfiguration(new File("myJetty.xml").toURL()); //or use new XmlConfiguration(new FileInputStream("myJetty.xml")); configuration.configure(server); server.start();
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:
// 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"); server.start();
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! 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
Comments (14)
May 24, 2006
Christian d'Heureuse says:
I have written an open-source application that uses Jetty6 embedded: http:...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.
May 28, 2006
Stefan Kleineikenscheidt says:
As long there is not tutorial you might want to have a look at: Quick Guide to E...As long there is not tutorial you might want to have a look at: Quick Guide to Embedding Jetty
Dec 12, 2006
Clinton Foster says:
In the jetty.xml example, the argument for the XmlConfiguration constructor shou...In the jetty.xml example, the argument for the XmlConfiguration constructor should be: new FileInputStream("myJetty.xml").
Dec 13, 2006
Jan Bartel says:
Clinton, Thanks for pointing this out. Fixed now. JanClinton,
Thanks for pointing this out. Fixed now.
Jan
Dec 13, 2006
Tim Barlotta says:
The example for MinimalServlets has an error. It never calls: handler.init...The example for MinimalServlets has an error. It never calls:
handler.initialize();
HTH,
Tim
Dec 14, 2006
Jan Bartel says:
Tim, fixed in svn trunk. BTW the fix was in the ServletHandler class to ensure t...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.
Jan 20, 2007
Damiano says:
I have ported my application from Jetty5 to Jetty6 and while doing so I "discove...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.
Jul 17, 2007
larry h. says:
For my test rig, I found the examples provided in the links a bit confusing. I h...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:
server = new Server(); Connector connector = new SelectChannelConnector(); connector.setPort(PORT); connector.setHost("127.0.0.1"); server.addConnector(connector); WebAppContext wac = new WebAppContext(); wac.setContextPath("/"); wac.setWar("./web"); // this is path to .war OR TO expanded, existing webapp; WILL FIND web.xml and parse it server.setHandler(wac); server.setStopAtShutdown(true); server.start();Consider adding an example which just does this most simple invocation.
larry
Aug 06, 2007
Alexander says:
I want to use Hessian protocol http://www.caucho.com/hessian/ between Server and...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:
<web-app> <servlet> <servlet-name>hello</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>home-class</param-name> <param-value>service.BasicService</param-value> </init-param> <init-param> <param-name>home-api</param-name> <param-value>service.Basic</param-value> </init-param> </servlet> <servlet-mapping> <url-pattern>/hello</url-pattern> <servlet-name>hello</servlet-name> </servlet-mapping> </web-app>How i can to pass this parameter for servlet over usual code (for example in my code in higher)???
Aug 08, 2007
Alik Eliashberg says:
For use of Jetty as an embedded container, where the full power of Servlets is n...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.
Aug 14, 2007
john doe says:
Running Apache Axis in Embedded Jetty import org.apache.axis.transport.http.Adm...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.
Sep 17, 2007
Ichiro Furusato says:
We've been using Jetty in an embedded application for over five years, but upon ...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
Oct 12, 2007
Christophe Hamerling says:
Hi Is it possible to add servlets to a context when the server is running ? Th...Hi
Is it possible to add servlets to a context when the server is running ?
Thanks
Nov 25, 2007
L. Oproman says:
Can someone help me put together an example of creating a URL rewriting handler ...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.