Jetty Logo
Version: 9.2.3.v20140905
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

Embedded Examples

Simple File Server
Split File Server
Multiple Connectors
Embedded SPDY Server
Secured Hello Handler
Minimal Servlet
Web Application
Web Application with JSP
Adding Examples

Jetty has a rich history of being embedded into a wide variety of applications. In this section we will walk you through a number of our simple examples under our embedded-jetty-examples project in our git repository.

Live Files

These files are pulled directly from our git repository when this document is generated. If the line numbers do not line up feel free to fix this documentation in github and give us a pull request, or at least open an issue to notify us of the discrepancy.

Simple File Server

This example shows how to create a simple file server in Jetty. It is perfectly suitable for test cases where you need an actual web server to obtain a file from, it could easily be configured to serve files from a directory under src/test/resources. Note that this does not have any logic for caching of files, either within the server or setting the appropriate headers on the response. It is simply a few lines that illustate how easy it is to serve out some files.

Example 25.1. FileServer.java


Run it!

After you have started things up you should be able to navigate to http://localhost:8080/index.html (assuming one is in the resource base directory) and you are good to go.

Maven Coordinates

To use this example in your project you will need the following maven dependencies declared.

Split File Server

This example builds on the Simple File Server to show how chaining multiple ResourceHandlers together can let you aggregate mulitple directories to serve content on a single path and how you can link these together with ContextHandlers.

Example 25.2. SplitFileServer.java


Run it!

After you have started things up you should be able to navigate to http://localhost:8090/index.html (assuming one is in the resource base directory) and you are good to go. Any requests for files will be looked for in the first resource handler, then the second, and so on and so forth.

Maven Coordinates

To use this example as is in your project you will need the following maven dependencies declared. We would recommend not using the toolchain dependency in your actual application.

Multiple Connectors

This example shows how to configure Jetty to use multiple connectors, specifically so it can process both http and https requests. Since the meat of this example is the server and connector configuration it only uses a simple HelloHandler but this example should be easily merged with other examples like those deploying servlets or webapps.

Example 25.3.  ManyConnectors.java


Walkthrough

Start things up! By using the server.join() the server thread will join with the current thread. See Thread.join() for more details.

Maven Coordinates

To use this example in your project you will need the following maven dependencies declared.

Embedded SPDY Server

This example shows how to create a server with a SPDY connector in Jetty. Clients (mostly browsers) that have a SPDY client implementation will talk SPDY to that connector. All others will transparently fall back to HTTP.

Example 25.4.  SpdyServer.java


Run it

After you have started things up you should be able to navigate to http://localhost:8080/index.html (assuming one is in the resource base directory) and you are good to go.

Maven Coordinates

To use this example in your project you will need the following maven dependencies declared.

Secured Hello Handler

This example shows how to wrap one handler with another one that handles security. We have a simple Hello Handler that just return a greeting but add on the restriction that to get this greeting you must authenticate. Another thing to remember is that this example uses the ConstraintSecurityHandler which is what supports the security mappings inside of the servlet api, it could be easier to show just the SecurityHandler usage, but the constraint provides more configuration power. If you don't need that you can drop the Constaint bits and use just the SecurityHandler.

Example 25.5. SecuredHelloHandler.java


Run it!

After you have started things up you should be able to navigate to http://localhost:8080/index.html (assuming one is in the resource base directory) and you are good to go.

The Realm Properties File

Maven Coordinates

To use this example in your project you will need the following maven dependencies declared.

Minimal Servlet

This example shows the bare minimum required for deploying a servlet into jetty. Note that this is strictly a servlet, not a servlet in the context of a web application, that example comes later. This is purely just a servlet deployed and mounted on a context and able to process requests. This example is excellent for situations where you have a simple servlet that you need to unit test, just mount it on a context and issue requests using your favorite http client library (like our Jetty client found inChapter 28, HTTP Client).

Example 25.6.  MinimalServlets.java


Walkthrough

Start things up! By using the server.join() the server thread will join with the current thread. See Thread.join() for more details.

It is really simple to create useful servlets for testing behaviors, sometimes you just need a http server to run a unit test against that will return test content and wiring up a servlet like this makes it trivial.

After you have started things up you should be able to navigate to http://localhost:8080/ and you are good to go.

Maven Coordinates

To use this example in your project you will need the following maven dependencies declared.

Web Application

This example shows how to deploy a simple webapp with an embedded instance of jetty. This is useful when you want to manage the lifecycle of a server programmatically, either within a production application or as a simple way to deploying and debugging a full scale application deployement. In many ways it is easier then traditional deployment since you control the classpath yourself, making this easy to wire up in a test case in maven and issue requests using your favorite http client library (like our Jetty client found in Chapter 28, HTTP Client).

Example 25.7. OneWebApp.java


Run it!

After you have started things up you should be able to navigate to http://localhost:8080/ and you are good to go.

Maven Coordinates

To use this example in your project you will need the following maven dependencies declared.

Web Application with JSP

This example is very similar to the one in the previous section, although it enables the embedded webapp to use JSPs. As of jetty-9.2, we use the JSP engine from Apache, which relies on a Servlet Specification 3.1 style ServletContainerInitializer to initialize itself. To get this to work with Jetty, you need to enable annotations processing, as shown in this example code:

Example 25.8. OneWebAppWithJsp.java


Run it!

After you have started things up you should be able to navigate to http://localhost:8080/jsp/ and click on any of the links to jsps.

Maven Coordinates

To use this example in your project, you will need the following maven dependencies declared, in addition to those from the previous section:

Adding Examples

If you would like to add an example to this list, fork the documentation project from github (see the blue bar at the bottom of this page) and add the new page. Feel free to add the example contents directly as a programlisting using language="java" and we will take it from there.

If you feel and example is missing, feel free to open a bug to ask for it. No guarantees, but the more helpful and demonstrative it is the better.

See an error or something missing? Contribute to this documentation at Github!(Generated: 2014-11-22T01:00:33-08:00)