I am trying to make an application with an embedded Jetty 12.0.7 that should serve static content like html and css files and dynamic like servlets and filters. The code I have, so far, goes here:
public static void init() throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);
// Create and configure a ResourceHandler.
WebAppContext handler = new WebAppContext();
// Configure the directory where static resources are located.
URL r = Start.class.getResource("/web/");
System.out.println(r);
handler.setBaseResource(ResourceFactory.of(handler).newResource(r));
server.setHandler(handler);
handler.addServlet(HelloServlet.class, "/hi");
handler.addFilter(LoggerFilter.class, "/*",
EnumSet.of(INCLUDE, ASYNC, FORWARD, ERROR, REQUEST));
server.start();
}
Now I want to add support to also have jsp files along with the other stuff under /web/ (which, by the way, is under the /src/main/resources/ path). Does anyone know how to add jsp support to the function above? My dependencies, so far, look like this:
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<jetty.version>12.0.7</jetty.version>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-apache-jsp</artifactId>
<version>${jetty.version}</version>
</dependency>
</dependencies>
I have been trying to make Jetty 'understand' jsp-files but to no success.
I'm not using WebAppContext
but ServletContextHandler
, but hopefully it won't be much different.
First, JettyJasperInitializer
has to be added in addition to JettyJspServlet
:
handler.addServletContainerInitializer(JettyJasperInitializer.class);
handler.addServlet(JettyJspServlet.class, "*.jsp");
But that's not enough actually, you also have to explicitly set the handler's class loader:
handler.setClassLoader(Thread.currentThread().getContextClassLoader());
(you could also use Start.class.getClassLoader()
)
I do also set a specific temporary directory, although it's not required:
var tempdir = Files.createTempDirectory("jsp").toFile();
tempdir.deleteOnExit();
handler.setAttribute(ServletContext.TEMPDIR, tempdir);
And of course you can configure various init parameters of the JettyJspServlet
(see documentation at https://eclipse.dev/jetty/documentation/jetty-12/operations-guide/index.html#og-configuration-of-the-jsp-servlet and default configuration –when using non-embedded Jetty, so to be replicated with embedded Jetty– at https://github.com/jetty/jetty.project/blob/ea8139e94a6918b6edb2a3a1e8e83af45f0057ab/jetty-ee10/jetty-ee10-webapp/src/main/config/etc/webdefault-ee10.xml#L181-L219). At a minimum you'll want to set "development"
to false
when running in production. The temporary directory above could also be configured as "scratchDir"
init parameter on the servlet.