Packaging a Web Application

Now that we’ve considered each of the pieces that constitute our ListManager web application, we’ll look at how those pieces are put together into a web application WAR file. For completeness, we’ll include all the servlets and JSP pages from this chapter, not just the ones that implement the mailing list manager. In order to do this, we must:

  1. Write a web.xml file that configures all the servlets

  2. Arrange that configuration file—along with the servlet implementation classes, JSP pages, and related files—in their correct places in a directory hierarchy

  3. Zip them all up (with the jar tool) to create a WAR file

Recall from the start of this chapter that we used these commands to compile the servlet classes and create the WAR file:

javac -d WEB-INF/classes/ *.java
jar cMf je3.war \
        index.html *.jsp *.jspx ListManager/*.jsp \
        WEB-INF/web.xml WEB-INF/tags/*.tag WEB-INF/classes

We’ll consider the directory hierarchy of the web application first, and then look at the web.xml file.

Web Application Directory Structure

Our je3 web application is structured as follows:

/

This is the root directory for the application. The servlet container will map a file x in this directory to a URL like this:

http://localhost:8080/je3/x
                        
/index.html

The default file served for the URL “je3/”. This is just a plain HTML file that contains links to the various examples contained in the web application.

/*.jsp

The hello.jsp, hello2.jsp and hello3.jspx examples go here.

/ListManager/

A directory for ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.