Jetty Servlet Example
In this example, we will show you how to make use of Jetty – Java HTTP Web Server and servlet container and run a sample servlet on this server. Jetty is an open source web server developed by Eclipse Foundation. As a part of this example we will create an eclipse project which will have our servlet code and to deploy on jetty, we will configure that project in a war file.
1. Environment
In this example, following environment will be used:
- Eclipse Kepler 4.3 (as IDE)
- Jetty – version 9.2.15 v20160210
- Java – version 7
- Java Servlet Library – servlet-api-3.0
2. Jetty Servlet Example
2.1 Structure of the example
In this example, we are going to write a simple servlet and run that servlet on Jetty web server. We will package our servlet project in a WAR file. We can then deploy this war file on running jetty server and it will dynamically detect our servlet.
2.2 Running Jetty
Make sure you download the correct version of Jetty from Download Jetty. Certain versions of jetty only run with certain versions of Java. You might run into an error java: unsupported major:minor version 52.0 . Once you extract downloaded jetty zip file on your machine, you can open a command prompt and navigate to directory /demo-base and run java -jar ../start.jar , this will start our jetty web server. To verify everything is alright with our jetty installation, launch a web browser and go to url http://localhost:8080, it should show a Jetty welcome page.
2.3 Creating Example Servlet
We will create a Dynamic Web Project in eclipse. Follow the steps below:
- Go to File -> New Project -> Web -> Dynamic Web Project
- Provide a name for your project “FirstServletJetty” and choose Target runtime as None
- Click Next. On Web Module screen, select the checkbox for “Generate web.xml deployment descriptor”

After creating our project, we will need to sort out dependencies to write our first servlet. Download servlet-api-3.0.jar file and import that in our project’s build path.

- Go to Src folder in project directory and right click to select New Servlet
- Enter package name: com.javacodegeeksexample
- Enter servlet name: FirstServlet
- Keep default options and click Finish
2.4 Modifying Example Servlet
Now we can write our code in the servlet we just created. We can write our code in doGet or doPost . We will write very simple print statement to see how our servlet behave once deployed on webserver.
This is how the final code of FirstServlet looks:
FirstServlet.java
package com.javacodegeeksexample;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FirstServlet
*/
@WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public FirstServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println("First Servlet on Jetty - Java Code Geeks");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
Save your source code file and build the project in eclipse.
2.5 Deploying your servlet on Jetty
- Save project -> Export -> Web -> WAR file






Doesn’t work.