Embedding Jetty with Netbeans 7 (and Maven)

Post to Twitter

I’m on a project right now where I need to embed Jetty 7, there are some good documentation and blog posts out there on how to do this as well as numerous examples provided here. Today I’ve taken some of the samples and modified them a little. I’ll take two servlets and serve them through Jetty 7 (embedded) using Netbeans 7 and Maven.


Using Netbeans 7 create a new Maven Java Application and call it EmbeddedJettyDemo. Right-click on the dependencies and select add dependency. In the query textbox use the text jetty-server and add in version 7.4.5 or later. Add another dependency and use jetty-servlet and add in the same version: 7.4.5 or later (make sure the version matches with the jetty-server you just added.

Note: If Jetty is not showing up for you then you might need to add additional Maven repositories.

Create two new Java class files in the default package called: HelloServlet.java and HelloServlet2.java

The code for the App.java class is as follows (this is the class we will run):

package com.giantflyingsaucer.embeddedjettydemo;

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

public class App
{
    public static void main( String[] args )
    {
        try {
            Server server = new Server(8080);

            ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
            context.setContextPath("/demo");
            context.addServlet(new ServletHolder(new HelloServlet()),"/*");
            context.addServlet(new ServletHolder(new HelloServlet("This is servlet 1")),"/1/*");
            context.addServlet(new ServletHolder(new HelloServlet2("This is servlet 2")),"/2/*");

            server.setHandler(context);
            server.setStopAtShutdown(true);
            server.start();
            server.join();
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

This is very basic and you can see it adds two servlets. The first servlet is mapped as: http://localhost:8080/demo/* and as http://localhost:8080/demo/1 The second servlet is mapped as: http://localhost:8080/demo/2

Here is the code for the two servlets:

HelloServlet.java

package com.giantflyingsaucer.embeddedjettydemo;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {

    String greeting = "Hello Servlet";

    public HelloServlet() {}

    public HelloServlet(String hi) {
        greeting = hi;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>" + greeting + " (HelloServlet)</h1>");
        response.getWriter().println("session=" + request.getSession(true).getId());
    }
}

HelloServlet2.java

package com.giantflyingsaucer.embeddedjettydemo;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class HelloServlet2 extends HttpServlet {

    String greeting = "Hello Servlet 2";

    public HelloServlet2() {}

    public HelloServlet2(String hi) {
        greeting = hi;
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<h1>" + greeting + " (HelloServlet2)</h1>");
        response.getWriter().println("session=" + request.getSession(true).getId());
    }
}

Run the project and try the following links:
http://localhost:8080/demo/
http://localhost:8080/demo/1
http://localhost:8080/demo/2

Results:

The source code is available here.

Note: If your curious to know how to shutdown embedded Jetty (other than killing the process) take a look at this blog post.

Post to Twitter

This entry was posted in Java, Netbeans, Open Source. Bookmark the permalink.