Java Web MVC Framework Fork me on GitHub
About Documentation Downloads Javadocs Source FAQ Feedback

Bootstrapping with Embedded Jetty

Instead of deploying a war file to a standalone servlet container, such as Tomcat, it's also possible to create a standalone application that embeds a servlet container. Jetty is the servlet container commonly used for this purpose. It's easy to bootstrap your application with Jetty and Mojave. To demostrate this, let's create a simple Hello World application that embeds a Jetty server, and handles requests through Mojave controllers.

NOTE: You'll need to have the Mojave jar and all its dependencies on the classpath for this demo application to compile and run. We're using Jetty 8 in this example, so be sure to add the Jetty jars to the classpath as well.

To start, create a package called 'demo.mojave.controllers', and add the following controller class to that package:

package demo.mojave.controllers;

import org.mojavemvc.annotations.Action;
import org.mojavemvc.annotations.StatelessController;
import org.mojavemvc.views.PlainText;

public class HelloWorld {

  public PlainText sayHello() {
    return new PlainText("Hello World!");

Next, in the 'demo.mojave' package, create the following Main class:

package demo.mojave;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

public class Main {

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

    Server server = new Server(8080);
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    HandlerList handlers = new HandlerList();
    handlers.setHandlers(new Handler[]{context, new DefaultHandler()});

    ServletHolder servletHolder = new ServletHolder(new org.mojavemvc.FrontController());
    servletHolder.setInitParameter("controller-classes", "demo.mojave.controllers");
    context.addServlet(servletHolder, "/*");


That's it! Now, just start the application from the command line, and in a browser, go to:


You should see the words 'Hello World!' displayed in the browser.

NOTE: In the example above, we set the URL pattern to "/*". However, if we had returned a JSP view, or otherwise used forward() or include() when rendering our views, we would need to use a pattern like "/serv/*".