This project is just a simple test of running an embedded Jetty server from within Java code.
These instructions assume you have Apache Ant installed. They also download version 7.1.6.v20100715 of the Jetty distribution (on which the project depends). Please substitute the file and folder names depending on which version you download.
- Clone the repository and enter the directory:
git clone git://github.com/BinaryMuse/embedded-jetty-test.git
cd embedded-jetty-test
- Download one of the Jetty distribution archives from
the Jetty 7.x Stable download page (using version 7.1.6.v20100715
here):
wget http://download.eclipse.org/jetty/stable-7/dist/jetty-distribution-7.1.6.v20100715.tar.gz
- Extract the archive:
tar -zxvf jetty-distribution-7.1.6.v20100715.tar.gz
- Copy the
.jar
files from the Jetty distribution to the repository'slib
folder:cp jetty-distribution-7.1.6.v20100715/lib/*.jar lib/
- Run the project:
ant
- To start on a port other than 8080 (for example, 8765):
ant -Dport=8765
You should be able to visit http://localhost:8080 in
your browser and view the working application. Quit the server by pressing
Ctrl+C
on the command line where you started the server or by visiting
http://localhost:8080/servlets/shutdown
The example app demonstrates a few interesting/useful things. Here is a list of all URI's:
- Serving files from a directory:
/
and/images
- Managing servlets based on file patterns:
/servlets/session
- Serving a webapp in war form:
/webapp
- Catching unhandled URIs: any non-handled URI (e.g.
/fake-uri
) - Serving servlet for shutting down the server:
/servlets/shutdown
Details on each follow:
EmbJetTest.java
contains code for setting up a file server. The file server
is given a directory to use as its "base" directory, as well as a few other
options (including a list of files to use as "welcome files").
EmbJetTest.java
sets up a handler to handle a simple servlet, SessionServlet
,
on the URI /servlets/session
. This servlet manages session objects as well as
a servlet object, shared amongst any instance of the servlet. Test the servlet
by opening the page in multiple browsers and refreshing. You can see that the
servlet state object is shared amongst all visitors.
Notice that the State object needs to be thread safe!
EmbJetTest.java
also sets up a WebAppContext
to serve up a web application
packaged in a war on a subdirectory (in this case webapp
). I used a GWT
application as a test.
By using a FourOhFourHandler
as the last item in the HandlerList
for the
server, we can effectively catch any URI that was not handled already and
generate a 404 for it.
By visiting /servlets/shutdown
, the server can be shut down. This
demonstrates the ability for servlets to interact with the main server
object.