Deploying a web application to Jetty

The Jetty web application server is great. It is just not very well document and when it is documented it is aimed for the very uninitiated. If you’re using Jetty, it is almost like an old boys club – ‘you made it’.

Still, it appears that Jetty and Tomcat are becoming much more similar in their way of doing things, with both using a somewhat proprietary, if you can say that on an open source project, configuration schemes. So how do you deploy a web application to Jetty?

  1. Create a WAR file for your application. This is standard and I assume you already know how to do this with Ant or inside of Eclispe (if you are truly lazy and I am often very lazy). Like any WAR file it will contain a deployment descriptor – web.xml – which will specify information about filters, servlets, jsp files and other standard configuration options.
  2. Like Tomcat, Jetty can use a context file to specify the server-specific configuration of the web application. In Jetty, context files live in the aptly named contexts folder. Each context file looks something like this (example taken from Jetty’s documentation):
    1
    2
    3
    4
    5
    6
    
    < ?xml version="1.0"  encoding="ISO-8859-1"?>
    < !DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
    <configure class="org.mortbay.jetty.webapp.WebAppContext">
      <set name="contextPath">/test</set>
      <set name="war"><systemproperty name="jetty.home" default="."/>/webapps/test.war</set>
    </configure>

    In this case, the contextPath element specifies the context by which users will find the application on your server; in this case http://[domain-name]/test/. The war element represents the location of the WAR file. Here, it is inside the Jetty home directory, under the standard webapps folder, using a WAR file called test.war. If your WAR file is exploded, just put the name of the folder under which the files reside.

  3. Save the context file with the same name as your web application WAR file inside the contexts directory.
  4. Put your WAR file in the location referenced in the context file (e.g. $JETTY_HOME/webapps/test.war)
  5. Start Jetty from the command line by calling
    java -jar start.jar
    when in the home Jetty directory.

I used Jetty 6.1.7 to verify the code.

In short, this is really easy.

Tags: , , , , , , , , , ,

12 comments

  1. Hi,

    in the context xml, “set” should be “Set”.

    thanks.

    may i quote this article in my blog? thanks

    Junmin

  2. Feel free to quote!

  3. Raj Kumar Kundu

    This is very useful information. But if I have to access images present in the application server and if the images are kept out side jetty directory then show can I access them using relative url?

  4. if you get this error: java.lang.ClassNotFoundException: org.mortbay.jetty.webapp.WebAppContext

    try to use: org.eclipse.jetty.webapp.WebAppContext

  5. […] Deploying a web application to Jetty […]

  6. Do you know how to hot-redeploy a servlet?

  7. if we follow the above mentioned procedure the test.war file will be deployed in /tmp folder. Is there any way to save the deployed file onto a specific folder

  8. Hi!
    Thanks a lot for this clear information. However, i am wondering, if i execute a .war on my Jetty server, does this means that i am not going to be able to retreive my ApplicationContext into my app ? Because Jetty is creating it for me.
    Is the only possibility then to embed Jetty and create manually in Java the applicationContext in order to be able to retrieve beans from it ?
    Thanks for you help!
    max

  9. Thank you for the article.

    I have installed the jetty-distribution-7.2.0.v20101020 and running on window7.
    I have created maven based web application mavenproject1.war and place it in wepapps directory it works fine.
    It failed to work for mavenprojext2 if add another war file eg. Mavenproject2.war.
    I have create two xml files(mavenproject1.xml, mavenproject2.xml) in context directory.

    These two files are based on text.xml which provide by the distribution.
    The part of the xml which has modified for mavenproject1.xml:
    /
    /webapps/mavenproject1.war

    The web.xml file for mavenproject1:
    < !DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" >




    HelloServlet
    com.mavenproject1.HelloServlet



    HelloServlet
    /HelloServlet



    I have changed mavenproject1.xml contextPath to: /mavenproject1 and it not working for mavenproject1.

    If you can help me to make working for both war files will much appreciated.

  10. I am trying to deploy an app to the root of the domain so instead of domain.com/war_app/ i would be just domain.com/ I tried to do this with your basic config file but i got a 404 you can see below.

    Error 404 – Not Found.
    No context on this server matched or handled this request.
    Can you help me?

  11. Hi… thanks for the blog…
    I have a problem… After we deploy the war and start the jetty server it will create the application folder right.. How do I set its path? I mean its currently creating the folder in windows/temp.. I want it in some other place… Please reply asap..
     
    Thanks in adv

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>