Maven by Example
   - 6.4. The Simple Web Application Module

6.4. The Simple Web Application Module

The simple-webapp module is the second submodule referenced in the simple-parent project. This web application project depends upon the simple-weather module, and it contains some simple servlets that present the results of the Yahoo weather service query.

simple-webapp Module POM. 

<project xmlns=""

    <name>simple-webapp Maven Webapp</name>

This simple-webapp module defines a very simple servlet that reads a zip code from an HTTP request, calls the WeatherService shown in The WeatherService Class, and prints the results to the response’s Writer.

simple-webapp WeatherServlet. 

package org.sonatype.mavenbook.web;

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

public class WeatherServlet extends HttpServlet {
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws ServletException, IOException {
        String zip = request.getParameter("zip" );
        WeatherService weatherService = new WeatherService();
        PrintWriter out = response.getWriter();
        try {
            out.println( weatherService.retrieveForecast( zip ) );
        } catch( Exception e ) {
            out.println( "Error Retrieving Forecast: " + e.getMessage() );

In WeatherServlet, we instantiate an instance of the WeatherService class defined in simple-weather. The zip code supplied in the request parameter is passed to the retrieveForecast() method and the resulting test is printed to the response’s Writer.

Finally, to tie all of this together is the web.xml for simple-webapp in src/main/webapp/WEB-INF. The servlet and servlet-mapping elements in the web.xml shown in simple-webapp web.xml map the request path /weather to the WeatherServlet.

simple-webapp web.xml. 

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

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

Become a Member

Are you a current user of: