Web Application Deployment


In the previous section, I mentioned the WEB-INF subdirectory. This is just one of several required components of a working JSP application. In this section, you'll learn what JSP expects to see and where it expects to see it.

Figure 5.4 shows a typical directory structure in JSP. At the top level is a directory, typically called webapps , which is the parent directory to all the applications installed on the JSP server. Beneath it are a number of subdirectories, each of which corresponds to a specific application. In this figure, only one subdirectory ( app2 ) has been fully expanded; the others have been shaded out for space.

Figure 5.4. A typical JSP deployment structure.

graphics/05fig04.gif

Under the app2 directory, you find the root of your JSP files. On a server, you would access them as http:// servername /app2/login.jsp , to use the login JSP as an example. From this directory, you can create other JSP subdirectories (such as admin , shown in this example). You also have a special directory called WEB-INF . The WEB-INF directory contains one very important file: web.xml . This is the file that configures the application and is used by the JSP server when initializing the application. Listing 5.16 shows the contents on a simple web.xml file.

Listing 5.16 web.xml
 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"                           "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app>   <welcome-file-list>     <welcome-file>Index.jsp</welcome-file>   </welcome-file-list> </web-app> 

All this web.xml does is define a welcome file, which is a file that can serve as the index file for a directory if no filename is specified. Even if all the web.xml file has is a start and end web-app tag, it must be in WEB-INF because it tells the JSP server to create a Web application instance when it starts.

Also in WEB-INF , you might find TLD files or other initialization files such as the Struts XML definition file.

Beneath WEB-INF are two critical directories: classes and lib . The classes directory is the root of a classpath that's used by the container for your Web application. This is normally where all the classes you've created for your application go.

The lib subdirectory holds JAR files. Any JAR file placed in this directory will become available as if the JAR file was on the classpath explicitly. The difference between lib and classes is that JAR files in classes won't be looked at, and .class files in lib won't be used.

In addition to application-specific lib and classes directories, there is usually a serverwide pair of directories that do the same thing, but for all applications. These directories are a useful place to put libraries that all applications will want to make use of, or to store a property file that you want to be independent of an application.

WAR Files

To make it easier to transport applications to new servers, JSP servers understand how to unpack a WAR file. A WAR (Web application resource) file is just a JAR file but with a different extension. Inside a WAR file, you'll find the entire contents of an application, relative to the application subdirectory. For example, if you did a listing of a WAR file build from the app2 application shown in the diagram, you'd see Listing 5.17.

Listing 5.17 A Typical WAR File
 WEB-INF/web.xml WEB-INF/struts.xml WEB-INF/lib/torque.jar WEB-INF/lib/struts.jar WEB-INF/lib/mm-mysql-2.0.jar WEB-INF/classes/taglib/metric/MetersToFeet.jsp WEB-INF/classes/taglib/metric/KilosToPounds.jsp WEB-INF/classes/taglib/metric/KmToMiles.jsp login.jsp logout.jsp viewcart.jsp checkout.jsp admin/deleteuser.jsp admin/adduser.jsp admin/viewaccount.jsp admin/processorders.jsp 

As you can see, the name of the application itself is left out; it is determined from the name of the WAR file. To deploy a WAR file, just place it in the webapps subdirectory and restart your JSP server. The server will automatically unpack the WAR file and deploy the application.

A WAR FILE GOTCHA

WAR files are great, but there's one gotcha to remember. Most JSP servers won't unpack a new WAR file over an old application directory. That means if you deploy shop.war (which will create a new subdirectory called shop ), and later upload a new copy of shop.war and place it in the webapps directory, it won't replace the old version. You must stop the server, delete the old shop subdirectory (using rm -r shop under Linux, for example), and then restart the server so that it can unpack the application freshly.



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net