Sunday, May 17, 2009

Simple Webapp Using Maven and Tomcat

As far as I can tell, the projects Maven will generate are complex and thus not suited to beginners or for simple projects.  Here we will be creating the simplest possible web app using some nice features Maven provides.

The directory structure we will use has a root directory we will name with the project name.  The necessary files are the pom.xml, and web.xml.

The pom.xml file is as follows.
<project xmlns="" xmlns:xsi=""
The web.xml file:
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

  <display-name>Archetype Created Web Application</display-name>
You may need to create the settings.xml file in your .m2 folder.  Add the necessary parts of this XML into the settings.xml file.  (Remember, this is Maven code, only for the developer's workstation so this solution does not expose user-ids and passwords in the runtime environment.)
<settings xmlns=""
A very simple servlet.
package com.bookstore;
import javax.servlet.*;                                                         
import javax.servlet.http.*;
 *  report on status of servlet 
public class App extends HttpServlet {
    public void doGet( HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println( "SimpleServlet Executed" );

Run the appropriate command to compile and deploy or redeploy the application.

mvn clean package tomcat:deploy

mvn clean package tomcat:redeploy
To view the output from the servlet, navigate your browser to http://localhost:8080/mavenquickstart/simple.