Home » Enterprise Java » jetty » Jetty JSP Example

About Ibrahim Tasyurt

Avatar photo
Ibrahim is a Senior Software Engineer residing in Ankara,Turkey. He holds BSc and MS degrees in Computer Engineering from Middle East Technical University(METU). Throughout his professional carrier, he has worked in Enterprise Web Application projects for public sector and telecommunications domains. Java EE, Web Services and Enterprise Application Integration are the areas he is primarily involved with.

Jetty JSP Example

JSP (JavaServer Pages) which is core part of Java EE, enables developers to create dynamic web content based on the Java Servlet technology. In this example, we are going to enable Jetty for JSP. We will start with Embedded mode of Jetty. We are going to initialize our embedded Jetty to run JSP pages. Thereafter we will continue with standalone mode and shortly mention the JSP configuration in standalone mode.

Jetty supports two JSP Engine implementations: Apache Jasper and Glassfish Jasper. Starting from Jetty version 9.2, the default and favored implementation is Apache Jasper. In this example we are going to use this one; however we will show how we can switch to Glassfish implementation in the standalone mode.

At this point, we have to mention that, this example should not be considered as a JSP tutorial but a demonstration of JSP on Jetty container.

1. Environment

In the example, following environment will be used:

  • Java 8 (Java 7 is also OK)
  • Maven 3.x.y
  • Eclipse Luna(as the IDE)
  • Jetty v9.2.11 (In Embedded Jetty example, we will add Jetty libraries through Maven)

2. JSP with Embedded Jetty

2.1 Structure of the Example

In this example, we are going enable JSP in an Embedded Jetty. We are going to implement a very simple JSP page which will demonstrate JSP and JSTL capabilities. We are going to package this application as a WAR file; so we will be able to drop and run it in a standalone Jetty.

2.2 Creating the Maven Project in Eclipse

We will create the Maven project in Eclipse, applying the steps below:

  1. Go to File -> New ->Other -> Maven Project
  2. Tick Create a simple project and press “Next”.
  3. Enter groupId as : com.javacodegeeks.snippets.enterprise
  4. Enter artifactId as : jetty-jsp-example
  5. Select packaging as “war”.
  6. Press “Finish”.

After creating our project, we are going to add following dependencies to our pom.xml:

  1. org.eclipse.jetty:jetty-server
  2. org.eclipse.jetty:jetty-webapp
  3. org.eclipse.jetty:jetty-annotations
  4. org.eclipse.jetty:apache-jsp
  5. jstl:jstl

The first dependency (jetty-server) is the core Jetty dependency. jetty-webapp is needed for creating Jetty web application context. jetty-annotations dependency can be viewed as a utility, which makes JSP initialization easier. apache-jsp dependency is the Apache implementation of JSP and finally jstl is the JSP standard tag library(version 1.2).

After adding the necessary dependencies, our pom.xml looks like:

		<!--Jetty dependencies start here -->

		<!-- Jetty Dependencies end here -->

		<!--Jetty Apache JSP dependency  -->

		<!-- JSTL Dependency -->


2.3 Configuring the Web Application

As mentioned above, we are going to configure a very simple JSP application which will demonstrate both JSP and JSTL capabilities. The steps needed are described below:

  1. Create the folder src/main/webapp under your project directory(if not exists).
  2. Create WEB-INF directory under src/main/webapp (if not exists).
  3. Create web.xml under src/main/webapp/WEB-INF.
  4. Create example.jsp under src/main/webapp.

The content of the web.xml to enable JSP can be viewed below:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	<display-name>JSP Example Application</display-name>

	<servlet id="jsp">


example.jsp is a simple JSP file which shows current date and outputs a literal text which is a JSTL expression. The content of the JSP file is as follows:


<%@page import="java.util.ArrayList"%>

<title>Java Code Geeks Snippets - Sample JSP Page</title>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

	<c:out value="Jetty JSP Example"></c:out>
	<br /> 
	Current date is: <%=new java.util.Date()%>

2.4 Enabling JSP programmatically

In this part, we are going to start an embedded Jetty server with the simple web application that we have configured in the previous section and thereafter we will enable JSP for our server. In order to keep things simple, we are going to implement our Jetty Server through our Main class of the project. You can see the JettyJspExampleMain class below, decorated with source code comments.


package com.javacodegeeks.snippets.enterprise.jettyjsp;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.WebAppContext;

public class JettyJspExampleMain {

	public static void main(String[] args) throws Exception {

		// 1. Creating the server on port 8080
		Server server = new Server(8080);

		// 2. Creating the WebAppContext for the created content
		WebAppContext ctx = new WebAppContext();
		//3. Including the JSTL jars for the webapp.
		//4. Enabling the Annotation based configuration
		org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
        classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
        classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");
        //5. Setting the handler and starting the Server


  1. First we initialize an embedded Server on port 8080.
  2. Then we initialize the web application context.
  3. In Step 3, we include jstl jar for our web application. If we skip this step, we will not be able to use JSTL tags in our JSP pages.
  4. In step 4, we enable annotation based configuration for our server. This part of the code looks a bit like magical snippet, which seems irrelevant with JSP configuration; however these three lines is the most crucial part for JSP configuration. When annotation configuration is enabled, JSP implementation is automatically discovered and injected to the server. Otherwise we would have to implement it manually.
  5. Step 5 includes the snippets for setting the context handler and starting the server.

2.5 Running the Application

When we run the application, our embedded server will start on port 8080. If we try to access http://localhost:8080/jetty-jsp-example/example.jsp we can see our simple JSP page:

Output of example.jsp

Output of example.jsp

In our webpage JSP, “Jetty JSP Example” text comes from a JSTL expression whereas current date is an outcome of a core JSP expression.

3. JSP in Standalone Jetty

In the previous sections, we have discussed how to enable JSP on an Embedded Jetty. In the standalone mode, it is very easy to run JSP. In standalone mode, JSP is enabled by default. All we have to do is, dropping the JSP web application WAR in the webapps directory of Jetty.

Jetty has a jsp module which is enabled by default. You can disable it via the start.ini file under JETTY_HOME removing the following line:


start.ini file has a line that sets Apache as the default JSP  implementation:


If we want to use Glassfish implementation for some reason, then we have to alter this line to:


4. Conclusion

In this example, we have discussed how we can configure Jetty for JSP. We have first demonstrated configuration for Embedded Jetty with a simple JSP application, thereafter we have briefly mentioned how JSP is configured for the standalone mode.

You can download the full source code of this example here: jetty-jsp-example

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
4 years ago

Hi Ibrahim, Thanks for the example. I am struggling to find one solution , in your example when I tried to import jetty server version like this. I am getting the below error. Added lines in jsp: Java Code Geeks Snippets – Sample JSP Page Current date is: Server Version: Error I am getting: org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: [16] in the generated java file: [C:\Users\vkarn\AppData\Local\Temp\jetty-\jsp\org\apache\jsp\example_jsp.java] Only a type can be imported. org.eclipse.jetty.security.LoginService resolves to a package Stacktrace: at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102) at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:212) at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457) at org.apache.jasper.compiler.Compiler.compile(Compiler.java:377) at org.apache.jasper.compiler.Compiler.compile(Compiler.java:349) at org.apache.jasper.compiler.Compiler.compile(Compiler.java:333) at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:600) at… Read more »