< Zurück | Inhalt | Weiter >

23.2.3 Packaging the Servlet

We will now package up the servlet, along with a simple startup page to invoke it. We’ll look at the WAR file and see how it’s built.


23.2.3.1 What Is in the WAR File

The other JAR-like file in the EAR is the WAR file. Let’s see what is in one of those (Table 23.3).

Notice that the WAR file puts its XML descriptor not in the META-INF

directory but in a WEB-INF directory along with the classes.


Table 23.3 Contents of the WAR file


image

image

Name


MANIFEST.MF


web.xml


jboss-web.xml


classes classes/.../*.class

*.jsp


image

*.php META-INF WEB-INF

Type

text XML XML

directory class

JSP


HTML

directory directory

Content

A standard JAR manifest; it can be empty or list the contents.

XML description of the Web application—servlet definitions, and so on.

Empty in our example—no JBoss-specific directives are used.

Directory structure for the Java class files. The various class files.

These are the JSP files that run as part of the Web application; note that they are in the top level of this directory structure, not in any subdirectory.

Any static HTML pages, too. A directory with other files. A directory with other files.


23.2.3.2 Weaving the Web

The web.xml file is the descriptor for the Web application part of all this. Using the servlet tag, it defines a servlet associating a name with this servlet (a name which can be used elsewhere in this XML file) and stating which Java class file is that servlet.

Then the servlet-mapping tag is used to map a URL pattern to a servlet. The URL pattern is the portion of the URL that signals to the server that the request is not for a simple HTML page, but rather for our servlet.

Example 23.6 is a sample web.xml; notice in particular how the mapping from URLs to the Java class is accomplished.


23.2.3.3 Connecting the Pieces

So now that you have seen all the pieces, know that you can edit the XML files with your favorite editor, and can build the JAR/WAR/EAR files with the jar command, it’s not that hard to put it all together. It is, however, tedious, and is well worth automating, at least with Ant.

The key to making it work, whether by hand or by automation, is a workable directory structure. The easiest way to construct JAR files is to have


image

Example 23.6 Sample web.xml file

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app PUBLIC

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">


<web-app>

<servlet>

<servlet-name>SessionServlet</servlet-name>

<display-name>Simple Session Servlet</display-name>

<servlet-class>com.jadol.budgetpro.SessionTestServlet</servlet-class>


<load-on-startup>1</load-on-startup>


</servlet>


<servlet-mapping>

<servlet-name>SessionServlet</servlet-name>

<url-pattern>/servlet/test</url-pattern>

</servlet-mapping>


<session-config>

<session-timeout>0</session-timeout>

</session-config>


</web-app>


image


a directory structure that mirrors the structure of the JARs that you are build- ing. But that arrangement is often not helpful for source management purposes. It is therefore not uncommon to have a source tree that reflects the project structure and a separate build directory that mirrors the JAR file directory lay- out. As classes are compiled, the class files are copied into the build directory along with copies of the XML, JSP, and other files. As a last step in the build process, the build directories are “jarred up” into WAR/JAR/EAR files.


23.3 DEPLOYING THE EAR


Deploying means getting your file(s) into the right place and dealing with the Web server to get your application up and running. For EJBs this includes the

image

23.3 Deploying the EAR 515

automatic construction of various components by the server. It’s not as daunt- ing as it sounds—at least not any more.