HelloServlet 3.0 Maven Project with Embedded Jetty 9 and spring-boot-maven-plugin

Yesterday evening I was playing around with my Raspberry Pi. I got Jetty 9 running on it with almost no pain, and then I was looking for my old HelloServlet 3.0 Maven project. But it appears I lost it. So I searched the Internet for a while for a modern simple servlet application and picked up a little here and there and ended up with the following – as small and compact as I could get it:

  • Maven based
  • Embedded jetty with executable war OR deployable war
  • Servlets 3.0 (no web.xml)

An executable war file means that you can run it directly without a web container:

$ java -jar helloservlet-1.0.war
2014-02-13 21:35:52.369:INFO:oejs.Server:main: jetty-9.1.2.v20140210
2014-02-13 21:35:52.498:INFO:oejw.StandardDescriptorProcessor:main: NO JSP Support for /, did not find org.apache.jasper.servlet.JspServlet
2014-02-13 21:35:52.558:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@15d023f2{/,null,AVAILABLE}
2014-02-13 21:35:52.576:INFO:oejs.ServerConnector:main: Started ServerConnector@77ed2fe7{HTTP/1.1}{0.0.0.0:8090}

The application HelloServlet hangs here and is now available at http://localhost:8090/

helloservlet


With jetty server embedded into the war, its size becomes 1.4 MB. That is larger than before but still so small that I could hardly see it on my 1000 GB disk. Actually it fits on a 3.5″ floppy, for those of you who are old enough to know what that is.

The HelloServlet class is looking like this:

@WebServlet(urlPatterns = "/hello")
public class HelloServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        response.getWriter().print("<html><body>" +
                "<h3>Hello Servlet</h3>" +
                "<form name=\"input\" method=\"post\">\n" +
                "Hello value: <input type=\"text\" name=\"helloValue\">\n" +
                "<input type=\"submit\" value=\"Submit\">\n" +
                "");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String value = request.getParameter("helloValue");
        System.out.println("doPost,helloValue=" + value);
        response.setContentType("text/html");
        response.getWriter().print("<html><body>helloValue=" + value + "</body></html>");
    }
}

It lays out a form and let you post a value.

When you start the application standalone, you will execute the Main class. It starts and sets up Jetty with HelloServlet like this:

public class Main {
    public static void main(String[] args) throws Exception {
        Server httpServer = new Server(8090);
        WebAppContext webAppContext = new WebAppContext();
        webAppContext.setContextPath("/");
        webAppContext.setBaseResource(Resource.newClassPathResource("web"));
        webAppContext.addServlet(HelloServlet.class.getName(), "/");
        webAppContext.setConfigurations(new Configuration[]{new WebXmlConfiguration()});
        HandlerList handlers = new HandlerList();
        handlers.setHandlers(new Handler[]{webAppContext, new DefaultHandler()});
        httpServer.setHandler(handlers);
        httpServer.start();
    }
}

If you deploy the war file on a standalone web container, the @WebServlet(urlPatterns="/hello") kicks in and the context root becomes http://localhost:8080/hello (assuming your web container is at localhost:8080).

The source structure is like this:

helloservlet structure

Lets walk though the Maven pom.xml.

  • <packaging>war</packaging> makes Maven create a war file instead of a jar
  • The section pluginRepositories is needed for spring-boot-maven-plugin below
  • We are using the servlet API, therefore javax.servlet-api is included (it is included in jetty-server but we should not rely on transitive dependencies)
  • jetty-server is what it appears to be
  • jetty-webapp is needed for configuring the web application
  • maven-war-plugin with failOnMissingWebXml=false is needed, or you have to include an empty web.xml file
  • maven-compiler-plugin is used to tell that we want to use 1.7 source
  • spring-boot-maven-plugin makes the war executable

Full code is available here.

IT Consultant at CAG Edge. Cloud and Continuous Delivery specialist, software developer and architect, Node.js, Java.

Publicerad i Java

Kategorier

WP to LinkedIn Auto Publish Powered By : XYZScripts.com