Friday, December 16, 2011

Running Wicket on Jetty

If you're only using the web functionality provided by an application server, or you want to optimize HTTP performance, it would be a good idea to consider embedding the Jetty HTTP server inside your application. This way, you don't even have to generate a WAR-file and a web.xml file. The application is just a basic Java application, which you can run on any server with a Java VM.

Maven setup

If you're using a Maven project, add the following dependencies to the pom-file in the "dependencies"-element in order to start working right away:



Minimal Wicket application

For our example, we use a minimal Wicket application that consists of one page.

The file:
package com.javaeenotes;

import org.apache.wicket.Page;
import org.apache.wicket.protocol.http.WebApplication;

public class ExampleWicketApplication extends WebApplication {

    public Class<? extends Page> getHomePage() {
        return HomePage.class;
The file:
package com.javaeenotes;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;

public class HomePage extends WebPage {
    public HomePage() {
        add(new Label("message", "Hello World!"));
The HomePage.html file:
  <span wicket:id="message"></span>

The final class we need is a main class with the static main-method, in which we tie the Wicket servlet to the Jetty server.

The file:
package com.javaeenotes;

import org.apache.wicket.protocol.http.WicketFilter;
import org.apache.wicket.protocol.http.WicketServlet;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;

public class Main {
    public static void main(String[] args) {
        // Object that holds, and configures the WicketServlet.
        ServletHolder servletHolder =
                new ServletHolder(new WicketServlet());
                WicketFilter.FILTER_MAPPING_PARAM, "/*");

        // Web context configuration.
        WebAppContext context = new WebAppContext();
        context.addServlet(servletHolder, "/*");
        context.setResourceBase("."); // Web root directory.

        // The HTTP-server on port 8080.
        Server server = new Server();
        SocketConnector connector = new SocketConnector();
        server.setConnectors(new Connector[]{connector});

        try {
            // Start HTTP-server.
        } catch (Exception e) {

Run the main method in a VM, and connect to port 8080 with a browser to check that the application is working.


  1. This comment has been removed by the author.

    1. Hello and thank you very much for such a good article! It really helped me a lot.

      There was a small problem when I tried to run code described here. Wicket started, but I stuck with error: "Last cause: Can not determine Markup. Component is not yet connected to a parent".

      After some research I found that *.html files are not copied to the build target.

      So I added a "src/main/java" directory to resources section in the *.pom, and it solved the problem.

      Maybe for an experienced professional it's obvious, but I think for newbies like me this information could be useful. Hope it helps somebody :)

  2. Hello and thank you very much for such a good article! It really helped me a lot.