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:
- Go to File -> New ->Other -> Maven Project
- Tick Create a simple project and press “Next”.
- Enter groupId as : com.javacodegeeks.snippets.enterprise
- Enter artifactId as : jetty-jsp-example
- Select packaging as “war”.
- Press “Finish”.
After creating our project, we are going to add following dependencies to our pom.xml:
- org.eclipse.jetty:jetty-server
- org.eclipse.jetty:jetty-webapp
- org.eclipse.jetty:jetty-annotations
- org.eclipse.jetty:apache-jsp
- 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:
<dependencies> <!--Jetty dependencies start here --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>9.2.11.v20150529</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>9.2.11.v20150529</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-annotations</artifactId> <version>9.2.11.v20150529</version> </dependency> <!-- Jetty Dependencies end here --> <!--Jetty Apache JSP dependency --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>apache-jsp</artifactId> <version>9.2.11.v20150529</version> </dependency> <!-- JSTL Dependency --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> </dependencies>
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:
- Create the folder src/main/webapp under your project directory(if not exists).
- Create WEB-INF directory under src/main/webapp (if not exists).
- Create web.xml under src/main/webapp/WEB-INF.
- 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" version="2.5"> <display-name>JSP Example Application</display-name> <servlet id="jsp"> <servlet-name>uu</servlet-name> <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> <init-param> <param-name>logVerbosityLevel</param-name> <param-value>DEBUG</param-value> </init-param> <init-param> <param-name>fork</param-name> <param-value>>false</param-value> </init-param> <init-param> <param-name>keepgenerated</param-name> <param-value>>true</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jsp</servlet-name> <url-pattern>*.jsp</url-pattern> <url-pattern>*.jspf</url-pattern> <url-pattern>*.jspx</url-pattern> <url-pattern>*.xsp</url-pattern> <url-pattern>*.JSP</url-pattern> <url-pattern>*.JSPF</url-pattern> <url-pattern>*.JSPX</url-pattern> <url-pattern>*.XSP</url-pattern> </servlet-mapping> </web-app>
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:
example.jsp
<%@page import="java.util.ArrayList"%> <html> <head> <title>Java Code Geeks Snippets - Sample JSP Page</title> <meta> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> </meta> </head> <body> <c:out value="Jetty JSP Example"></c:out> <br /> Current date is: <%=new java.util.Date()%> </body> </html>
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.
JettyJspExampleMain.java
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(); ctx.setResourceBase("src/main/webapp"); ctx.setContextPath("/jetty-jsp-example"); //3. Including the JSTL jars for the webapp. ctx.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",".*/[^/]*jstl.*\\.jar$"); //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 server.setHandler(ctx); server.start(); server.join(); } }
- First we initialize an embedded Server on port 8080.
- Then we initialize the web application context.
- 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.
- 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.
- 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:
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:
--module=jsp
start.ini file has a line that sets Apache as the default JSP implementation:
jsp-impl=apache
If we want to use Glassfish implementation for some reason, then we have to alter this line to:
jsp-impl=glassfish
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
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-0.0.0.0-8083-webapp-_jetty-jsp-example-any-2747544101971628296.dir\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 »