Example of Embedded Jetty with JSP support
Switch branches/tags
Nothing to show
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Updating to Jetty 9.4.5 and simplifying Main May 31, 2017
.gitignore Updating gitignores Mar 10, 2016
README.md jsr199 property not used by apache jasper. Jun 1, 2017
pom.xml Updating to Jetty 9.4.12.20180830 Sep 24, 2018

README.md

Example: Embedded Jetty w/ JSP Support

This is a maven project, to build it:

$ mvn clean package

To run the example org.eclipse.jetty.demo.Main:

$ mvn exec:exec

Open your web browser to:

http://localhost:8080/  

To stop Jetty:

use CTRL+C

Code Of Interest

See org.eclipse.jetty.demo.Main

Set a Servlet Temp Directory

It is important for JSP to define a temp directory suitable for managing itself. Such a directory will be used for converting the JSP source into a java file and then compiling it into a class. A sub directory in this temp directory will be automatically added by the JSP implementation for loading the compiled JSP classes.

context.setAttribute("javax.servlet.context.tempdir",scratchDir);

Set a non-System Classloader

The JSP implementation will refuse to the System Classloader (per JSTL + JSP spec), this will wrap the system classloader in a simple URLClassLoader suitable for use by the JSP implementation.

// Set Classloader of Context to be sane (needed for JSTL)
// JSP requires a non-System classloader, this simply wraps the
// embedded System classloader in a way that makes it suitable
// for JSP to use
ClassLoader jspClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader());
context.setClassLoader(jspClassLoader);

Jsp Servlet must be named "jsp"

The JspServlet must be named "jsp" (per JSP spec).

// Add JSP Servlet (must be named "jsp")
ServletHolder holderJsp = new ServletHolder("jsp",JspServlet.class);
holderJsp.setInitOrder(0);

Default Servlet must exist

The JSP implementation relies on various Servlet Spec requirements, but mainly the fact that a "default" named servlet must exist.

// Add Default Servlet (must be named "default")
ServletHolder holderDefault = new ServletHolder("default",DefaultServlet.class);
holderDefault.setInitParameter("resourceBase",baseUri.toASCIIString());
holderDefault.setInitParameter("dirAllowed","true");
context.addServlet(holderDefault,"/");