SpringMVC with Embedded Jetty

As an accidental Java developer I’ve never been comfortable deploying applications into a container, especially when the web interface is secondary to the primary purpose of the application. Instead I prefer to programatically create and manage the web interface rather than have it manage me. Currently SpringMVC is my Java web framework of choice (due to company convention more than anything else) and it is built around the idea of being managed by a container such as Jetty or Tomcat.

While there is no shortage of existing posts on using SpringMVC with embedded Jetty, they fail for me due to the following reasons:

  • They embed Jetty just enough to bootstrap something that looks like a classic Java web application – not what I want!
  • They don’t address JSPs – the default view technology used by SpringMVC.

Compile Those JSPs

By compiling the JSPs prior to deployment we can save ourselves from the hassle of classpath issues, especially relating to tag libs.


This plugin will compile all the JSPs found under src/main/resources/webapp and modify the existing web.xml to direct requests for the JSPs to the pre-compiled versions. This avoids compiling them at runtime and speeds up the time it takes to respond to the first request for a JSP.

You may also notice that the JSPs are under src/main/resources/webapp instead of the more common src/main/webapp. This is to avoid extra lines in the pom.xml to pull in webapp as a resource, as we are packaging as a jar.

Bootstrapping Jetty

I use Spring annotations to configure Jetty and import its @Configuration class into my root context configuration class. In order for the SpringMVC dispatcher servlet to access beans created in the root context, this class needs to be application context aware:

public class JettyConfiguration implements ApplicationContextAware {
private ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;

this is used as the parent context for the new context that will be created for the SpringMVC dispatcher servlet:

public ServletHolder dispatcherServlet() {
AnnotationConfigWebApplicationContext ctx =
new AnnotationConfigWebApplicationContext();
DispatcherServlet servlet = new DispatcherServlet(ctx);
ServletHolder holder = new ServletHolder("dispatcher-servlet",
return holder;

There are still a few more tricks required to get the dispatcher servlet registered without defining it in your web.xml. We must also create a Jetty web application context, this creates the JSP servlet as well as a default servlet as would be created during the instantiation of a classic war based application:

public WebAppContext jettyWebAppContext() throws IOException {
WebAppContext ctx = new WebAppContext();
ctx.setWar(new ClassPathResource("webapp").getURI().toString());

/* We can add the Metrics servlet right away. */
ctx.addServlet(AdminServlet.class, "/metrics/*");

return ctx;

Notice that I’ve added the metrics servlet, but have yet to add the dispatcher servlet. For some reason, adding the dispatcher servlet here causes JSP views to fail. One option is to fall back to defining the dispatcher servlet in your web.xml (which may be the cleanest option), or you can register the dispatcher servlet in the Jetty lifeCycleStarted callback which we’ll do here:

public LifeCycle.Listener lifeCycleStartedListener() {
return new AbstractLifeCycle.AbstractLifeCycleListener() {
public void lifeCycleStarted(LifeCycle event) {
try {
ServletHolder dispatcherServlet = dispatcherServlet();
.addServletWithMapping(dispatcherServlet, "/");
} catch (Exception e) {
"Failed to start Spring MVC dispatcher servlet", e);

@Bean(initMethod = "start", destroyMethod = "stop")
public Server jettyServer() throws IOException {
Server server = new Server();

/* Add a life cycle listener so we can register the SpringMVC dispatcher
* servlet after the web application context has been started. */

return server;

In summary, this is just another variation of James Ward’s post on Containerless Spring MVC. The complete code for this project (which I use as a template) can be at https://github.com/jasonish/jetty-springmvc-jsp-template. The code as referenced in this post can be found in the 20121207 tag.

Next – As I’m not really a fan of JSPs, or the extra hoops required to make this work right I’ll probably look at updating the template with Thymeleaf support, which should greatly simplify things.


5 thoughts on “SpringMVC with Embedded Jetty

  1. Hello. I try to modify u temmplate for using tags like

    User: miquel

    And i have some problems: TemplateProcessingException: No visible SecurityExpressionHandler instance could be found in the application context

    I move importing of SecurityConfiguration.class into jettyWebAppContext();

    AnnotationConfigWebApplicationContext webApplicationContext
    = new AnnotationConfigWebApplicationContext();

    And my problem resolved. But i think it’s bad way to load config classes in different places. Mb any idea ?

    • With embedded Jetty, I can never have it as clean as I wanted. Spring Boot helps a lot, and I’m only using it these days instead of embedding Jetty myself.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s