Maven by Example
   - 5.5. Adding a Simple Servlet

5.5. Adding a Simple Servlet

A web application with a single JSP page and no configured servlets is next to useless. Let’s add a simple servlet to this application and make some changes to the pom.xml and web.xml to support this change. First, we’ll need to create a new package under src/main/java named org.sonatype.mavenbook.web:

$ mkdir -p src/main/java/org/sonatype/mavenbook/web
$ cd src/main/java/org/sonatype/mavenbook/web

Once you’ve created this package, change to the src/main/java/org/sonatype/mavenbook/web directory and create a class named SimpleServlet in, which contains the code shown in the SimpleServlet class:

SimpleServlet Class. 

package org.sonatype.mavenbook.web;

import javax.servlet.*;
import javax.servlet.http.*;

public class SimpleServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        out.println( "SimpleServlet Executed" );

Our SimpleServlet class is just that: a servlet that prints a simple message to the response’s Writer. To add this servlet to your web application and map it to a request path, add the servlet and servlet-mapping elements shown in the following web.xml to your project’s web.xml file. The web.xml file can be found in src/main/webapp/WEB-INF.

Mapping the Simple Servlet. 

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

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

Everything is in place to test this servlet; the class is in src/main/java and the web.xml has been updated. Before we launch the Jetty plugin, compile your project by running mvn compile:

~/examples/ch-simple-web/simple-webapp $ mvn compile
[INFO] [compiler:compile]
[INFO] Compiling 1 source file to \
[INFO] -----------------------------------------------------
[INFO] -----------------------------------------------------
[INFO] Compilation failure

/src/main/java/org/sonatype/mavenbook/web/[4,0] \
package javax.servlet does not exist

/src/main/java/org/sonatype/mavenbook/web/[5,0] \
package javax.servlet.http does not exist

/src/main/java/org/sonatype/mavenbook/web/[7,35] \
cannot find symbol
symbol: class HttpServlet
public class SimpleServlet extends HttpServlet {

/src/main/java/org/sonatype/mavenbook/web/[8,22] \
cannot find symbol
symbol  : class HttpServletRequest
location: class org.sonatype.mavenbook.web.SimpleServlet

/src/main/java/org/sonatype/mavenbook/web/[9,22] \
cannot find symbol
symbol  : class HttpServletResponse
location: class org.sonatype.mavenbook.web.SimpleServlet

/src/main/java/org/sonatype/mavenbook/web/[10,15] \
cannot find symbol
symbol  : class ServletException
location: class org.sonatype.mavenbook.web.SimpleServlet

The compilation fails because your Maven project doesn’t have a dependency on the Servlet API. In the next section, we’ll add the Servlet API to this project’s POM.

Become a Member

Are you a current user of: