![]() |
|
|
Simplify Java Web App Deployment with WAR Files by Richard Shaler [The code for this article is available for download.]
If you've read much at all about Java, it's likely you've seen at least one of these terms: JAR files, WAR files, or EAR files. JAR stands for Java ARchive, WAR stands for Web ARchive, and EAR stands for Enterprise ARchive. In this article, I'll review all three archive types but focus mostly on WAR files. I'll describe how WAR files simplify Java Web application deployment, and show you how to use them to deploy an application to Tomcat for iSeries. The ARchive Trio Here, the term archive means library. JAR, WAR, and EAR files are a library, or package, of objects, typically those comprising an application. A Java archive resides in a file with a .jar, .war, or .ear extension. All three archive types use the same compression format as ZIP files. This allows you to use ZIP utilities such as WinZip to view Java archive contents. The most basic Java archive is the JAR file, which is (usually) a compressed collection of related Java classes. JAR files can be used to contain a Java application or a set of related utility functions (e.g., the iSeries Toolbox for Java). JAR files also include meta-data about its contents, such as application configuration information. Once packaged, the classes in a JAR file can be easily accessed by any Java application that is aware of the location of the JAR file. Any object within the JAR file can be used in its archived state; there's no need to decompress and unpackage the archive. Instead of managing dozens or hundreds of application objects, you only need to manage a single archive file. JAR files worked great for Java applications made up of mostly Java classes. But when JavaServer Pages and servlet technology came along, the need arose to package additional application objects such as JSPs, servlets, and HTML and XML files. Also, Java Web applications require more complex configuration information. JAR files weren't designed to handle the additional object types and the more complex configuration information of Java Web applications. That is why WAR files were created. With WAR files you can package all the resources of a Web application into a single (.war) file, which can be deployed to most standard Web application servers, including Tomcat. WAR files are useful with JSPs and servlet applications, but they fall short when it comes to Java 2 Enterprise Edition (J2EE) applications. That is where EAR files come into play. EAR files are used in an enterprise environment where multiple JAR and WAR files are required, as well as even more complex application configuration information. That's it for the Java archives review. The remainder of this article focuses on WAR files and how to use them with Tomcat on the iSeries platform. Tomcat Application Directory Structure If Tomcat is installed on your system, you will find a Tomcat home directory. The Tomcat home directory is simply a reference point from which a number of other special Tomcat directories reside. Within these directories your Java Web application resources reside. On a Windows system, Tomcat might reside in a home directory by the name of Apache Tomcat 4.0, as illustrated here: ![]() On the iSeries, the Tomcat configuration is slightly different. First, you must configure an HTTP server (powered by Apache) to support Tomcat. By default an HTTP pbA server creates a directory with the same name as the HTTP instance within the www directory of the OS/400 IFS root file system. Once you create the HTTP server instance, you configure Tomcat for that instance. For example, for this article I created an HTTP pbA server instance called httppba, which created an httppba directory within the www directory. I then configured Tomcat for the httppba server instance. The httppba directory is actually the home directory for the HTTP pbA server and Tomcat. You can see the httppba directory in the illustration here: ![]() In both Tomcat installation illustrations above, you'll see a webapps directory under the Tomcat home directory. It is within the webapps directory that Web applications reside. You can see that both illustrations contain a directory by the name of examples, under the webapps directory. The examples directory represents a Java application known as examples. Within every Java application directory there is a special directory called WEB-INF. This directory contains two special directories: classes and lib. The classes directory contains your Java application classes such as servlets and JavaBeans. The lib directory contains any Java packages (JAR files) used by the application. WEB-INF also contains a special application configuration file (web.xml) that provides application configuration information to the Tomcat server. You can see the WEB-INF directory in both Tomcat installations shown above. Application Deployment with Tomcat Java Web applications typically consist of many components that need to reside in certain directories of a Web application server. To deploy an application (meaning copying and configuring resources) to a Web application server requires a number of steps. For example, with Tomcat you must create a directory structure under the webapps directory to contain your application resources, and you must setup and modify special configuration files (e.g., web.xml) to enable the application. One way to deploy a Java application is to do it manually. If you select the manual method, you probably would create a customized script in a batch file to perform the required steps. Writing custom scripts may get the job done, but handling dozens and dozens of resources can be a lot of work. Custom scripts are not very reusable. For example, they are dependent on source and target system locations. Also, as your application evolves, so must your script. You may spend considerable time writing and tweaking deployment scripts. Another deployment method would be to use WAR files. WAR files can save you a lot of effort, especially if you need to deploy to multiple servers. Using a WAR file, you can eliminate the need to write deployment scripts. The WAR file basically says to the Web application server, "These are my contents, and this is how they are arranged." This allows the application to self-deploy. For instance, with the Tomcat server, all you need to do is copy the WAR file to the appropriate directory, restart Tomcat, and your application will automatically be deployed; the directory structure for the application will be created and the resources will be placed in the appropriate directories. (One other deployment method worth mentioning is using Apache Software Foundation's open-source ANT tool. With this tool you can build (compile) and deploy a Java application, including those that use JAR, WAR, and EAR files. ANT is definitely a tool you want to consider for application deployment, but it is beyond the scope of this article. For more information on ANT, go to the Apache Web site. Look for more on ANT and the WebSphere Development Studio tool's new Eclipse initiative when IBM releases V5R2 later on this summer! Sample Deployment In this section I will show you how to deploy an application to Tomcat for iSeries by using a WAR file. I deployed a Java application called examples, which contains the JSP and servlet application examples normally included with Tomcat. These examples don't seem to be included with Tomcat for iSeries, so I included them in the download package for this article. In fact, the download package is simply the WAR file named examples.war. Once you obtain the examples.war file, simply place it in the webapps directory of your Tomcat server (for the iSeries, this will be an HTTP pbA server). You can see the examples.war file in my webapps directory (in the right frame of Windows Explorer) in the illustration here: ![]() Once the WAR file is placed in your webapps directory, all you need to do is restart Tomcat (on the iSeries, this means restarting the HTTP pbA server configured for Tomcat), and the WAR file will be expanded, creating all of the directories and subdirectories and installing the objects required for your application to run. You can see the examples directory structure (look under the webapps directory) that was created when I restarted my HTTP pbA server in this illustration: ![]() Even though WAR files simplify deployment, some Web server configuration may still be required. With Tomcat for iSeries, you must configure what is called a URL mount point and an application context entry. Load the iSeries HTTP server administration application in your browser (http:// ![]() Now scroll down the configuration page to the Application contexts section. Click the Add button at the bottom of the context entry table. Enter /examples for the URL path value and webapps/examples for the Application base directory value. Now click the Continue button, and the entry will be added. You can see the entry I created for the examples application in this illustration: ![]() After you modify your Tomcat configuration, you must restart the HTTP pbA server in order for the changes to take effect. You'll find a Restart button at the top of the global settings configuration page of your HTTP pbA server. You can also restart an HTTP server from the Manage HTTP Servers page or from an iSeries command line with the STRTCPSVR command. After the Tomcat server restarts, the examples application is now deployed and ready to run. There are two main directory pages for the examples application: a directory of sample JSP applications and a directory of servlet applications. To display the JSP samples, open a browser session and enter a URL that points to the JSP index page. For example, I keyed in the following URL: http://s103d64g:8085/examples/jsp/index.html. My system name is s103d64g and the HTTP pbA server instance I configured for Tomcat uses port 8085. The examples/jsp/index.html is the path to, and the index of, JSPs contained in the examples application. The page is illustrated here: ![]() As you can see, there are links that will allow you to execute each application and links that allow you to display the source code. To display the servlet samples, enter a URL that points to the servlet index page. I entered http://s103d64g:8085/examples/servlets/index.html. You will see a page similar to the one shown in the previous illustration, except the list will contain servlet applications. Simplify with WAR Files If you examine the directory structure of the examples application deployed here, you will discover quite a number of directories and objects within the directories. It's easy to see that deploying this application manually would require a considerable amount of work. Using WAR files can decrease the effort. Of course, you must create a WAR file before you can deploy it. Here, I provided the WAR file (examples.war). Before you can deploy your own Java Web applications, you will need to learn how to create your own WAR files. That will be the subject of my next article.
|
Editors Contact the Editors |
Last Updated: 5/22/02 Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved. |