The Maven Cookbook

Chapter 6. Web Development

Chapter 6. Web Development

This is a test

6.1. Running a Web Application in a Servlet Container

6.1.1. Task

You need to run a web application in a servlet container.

6.1.2. Action

Configure your web application's Maven project to inlcude the Maven Jetty plugin as shown in the following POM:

<project xmlns="" 
  xsi:schemaLocation=" ">
  <name>sample-web Maven Webapp</name>

To start the web application in Jetty, run the run goal from the Maven Jetty plugin by running mvn jetty:run.

$ mvn jetty:run
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building sample-web Maven Webapp
[INFO]    task-segment: [jetty:run]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing jetty:run
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered 
resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (MacRoman actually) to copy filtered 
resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory ~/maven-cookbook/
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] [jetty:run {execution: default-cli}]
[INFO] Configuring Jetty for project: sample-web Maven Webapp
[INFO] Webapp source directory = ~/maven-cookbook/mcookbook-examples/web/
[INFO] Reload Mechanic: automatic
[INFO] Classes = ~/maven-cookbook/mcookbook-examples/web/sample-web/
2009-11-28 19:25:18.129:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
[INFO] Context path = /sample-web
[INFO] Tmp directory =  determined at runtime
[INFO] Web defaults = org/mortbay/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = ~/maven-cookbook/mcookbook-examples/web/sample-web/
[INFO] Webapp directory = ~/maven-cookbook/mcookbook-examples/web/sample-web
[INFO] Starting jetty 6.1.22 ...
2009-11-28 19:25:18.231:INFO::jetty-6.1.22
2009-11-28 19:25:18.405:INFO::No Transaction manager found - if your webapp 
requires one, please configure one.
2009-11-28 19:25:18.800:INFO::Started SelectChannelConnector@
[INFO] Started Jetty Server

At this point, you can use a web browser to navigate to http://localhost:8080/sample-web/ to interact with the web application.

6.1.3. Detail

Consider a simple web application with a single index.jsp page that contains a form, and a single Servlet that calculates a number from the Fibonacci sequence.

Example 6.1. Simple Form Accepting an Index to Pass to the Fibonacci Servlet

<h2>Fibonacci Page</h2>
<form action="fib" method="GET">
  <p>Fetch Fibonacci Sequence Index:
  <input type="text" name="index" size="5"/></p>
  <input type="submit" value="Calculate"/>

The following class is the Servlet which calculates the Fibonacci sequence. It takes a single parameter index and simply prints of the number at the specified position of the Fibonacci sequence.

Example 6.2. Fibonacci Servlet which Calculates a Number from the Fibonacci Sequence

package org.sonatype.mcookbook;

import java.util.Enumeration;

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

public class FibonacciServlet extends HttpServlet {

  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    int index = Integer.parseInt(req.getParameter("index"));
    resp.getWriter().write( fib(index) + "" );

  public long fib(int n) {
    if (n <= 1) return n;
    else return fib(n-1) + fib(n-2);

The following web.xml configures the FibonacciServlet to respond to the request path <context>/fib.

Example 6.3. Web Application Descriptor for sample-web

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

  <display-name>Archetype Created Web Application</display-name>

After running mvn:jetty, you can load the initial form by going to http://localhost:8080/sample-web/index.jsp. Populating the form with an index and pressing calculate will load the Fibonacci servlet and print out the number at that position of the sequence.