Understanding the Flow of Execution


Compiling, Packaging, and Running the Application

Now that you have examined the sample application in detail, it's time to compile, package, and run the application. First, though, download and install the Struts and Tomcat software if you have not already done so. Tomcat is a free servlet container available for download from the Internet and will be used in the examples in this book for running Struts applications. Tomcat is also the reference implementation for the JSP and servlet specifications from Sun. Of course, you don't have to use Tomcat to run the examples, but it is the only method described by this book. So, to follow along, it is strongly suggested that you use Tomcat.

Each of the following sections is dedicated to a step of the process. First, you will set up the Struts and Tomcat software. Next, you'll compile the application. Then, you'll package the application in a standard Web Archive file. Finally, you'll see how to deploy and run the application with Tomcat.

Downloading and Installing Struts and Tomcat

As mentioned, both Struts and Tomcat are freely available for download from the Internet. Following are the Web sites for each:

  • Struts http://struts.apache.org/

  • Tomcat http://tomcat.apache.org/

After you have downloaded the Struts and Tomcat software distributions, you will need to choose a directory to install them to. After selecting a directory, extract the files of each distribution to that directory. For example, if you choose to install the distributions in a directory called c:\java, then the Struts files would be located at c:\java\struts-1.3.5 (or similar) and Tomcat would be installed at c:\java\apache-tomcat-5.5.17 (or similar).

Compiling the Application

The Mini HR application consists of several files; however, only the Java source code files need to be compiled before you package and run the application. Because the Java source code files use the servlet and Struts APIs, you need to add these libraries to your Java classpath. You could do this by updating your CLASSPATH environment variable. Alternatively, you can just specify the path when you compile the Mini HR application.

In addition to the files that you created and reviewed earlier in this chapter, you also need to copy the following files to the c:\java\MiniHR\WEB-INF\lib directory. These .jar files contain the Struts and associated library class files that are necessary for the Mini HR application to run once it is packaged as a .war file.

c:\java\struts-1.3.5\lib\antlr-2.7.2.jar c:\java\struts-1.3.5\lib\bsf-2.3.0.jar c:\java\struts-1.3.5\lib\commons-beanutils-1.7.0.jar c:\java\struts-1.3.5\lib\commons-chain-1.1.jar c:\java\struts-1.3.5\lib\commons-collections-2.1.jar c:\java\struts-1.3.5\lib\commons-digester-1.6.jar c:\java\struts-1.3.5\lib\commons-fileupload-1.1.1.jar c:\java\struts-1.3.5\lib\commons-io-1.1.jar c:\java\struts-1.3.5\lib\commons-logging-1.0.4.jar c:\java\struts-1.3.5\lib\commons-validator-1.3.0.jar c:\java\struts-1.3.5\lib\oro-2.0.8.jar c:\java\struts-1.3.5\lib\struts-core-1.3.5.jar c:\java\struts-1.3.5\lib\struts-el-1.3.5.jar c:\java\struts-1.3.5\lib\struts-extras-1.3.5.jar c:\java\struts-1.3.5\lib\struts-faces-1.3.5.jar c:\java\struts-1.3.5\lib\struts-scripting-1.3.5.jar c:\java\struts-1.3.5\lib\struts-taglib-1.3.5.jar c:\java\struts-1.3.5\lib\struts-tiles-1.3.5.jar
Note 

The preceding .jar files are those packaged with Struts 1.3.5 and may change over time with newer versions of Struts. If you are using a different version of Struts, you should use the .jar files included with that version of Struts.

Assuming that you have installed Struts at c:\java\struts-1.3.5, installed Tomcat at c:\ java\apache-tomcat-5.5.17, and placed the Mini HR application files at c:\java\MiniHR, the following command line will compile the Mini HR application when run from the c:\ java\MiniHR directory:

javac -classpath WEB-INF\lib\antlr-2.7.2.jar;                  WEB-INF\lib\bsf-2.3.0.jar;                  WEB-INF\lib\commons-beanutils-1.7.0.jar;                  WEB-INF\lib\commons-chain-1.1.jar;                  WEB-INF\lib\commons-collections-2.1.jar;                  WEB-INF\lib\commons-digester-1.6.jar;                  WEB-INF\lib\commons-fileupload-1.1.1.jar;                  WEB-INF\lib\commons-io-1.1.jar;                  WEB-INF\lib\commons-logging-1.0.4.jar;                  WEB-INF\lib\commons-validator-1.3.0.jar;                  WEB-INF\lib\oro-2.0.8.jar;                  WEB-INF\lib\struts-core-1.3.5.jar;                  WEB-INF\lib\struts-el-1.3.5.jar;                  WEB-INF\lib\struts-extras-1.3.5.jar;                  WEB-INF\lib\struts-faces-1.3.5.jar;                  WEB-INF\lib\struts-scripting-1.3.5.jar;                  WEB-INF\lib\struts-taglib-1.3.5.jar;                  WEB-INF\lib\struts-tiles-1.3.5.jar;                  C:\java\apache-tomcat-5.5.17\common\lib\servlet-api.jar                     WEB-INF\src\com\jamesholmes\minihr\*.java                     -d WEB-INF\classes

Notice that you must specify the path to each .jar file explicitly. Of course, if you update CLASSPATH, this explicit specification is not needed. You should also notice that the compiled code will be placed into the WEB-INF\classes directory, as specified by the -d WEB-INF\classes section of the command line. Remember from the earlier discussion that this is the standard Web Archive directory that servlet containers will look in for compiled Web application code.

To simplify the process of building the Mini HR application in this chapter and in subsequent exercises in the book, on Windows-based systems you can place the preceding command line into a batch file named build.bat. The build.bat batch file can then be used to compile the application. All you have to do is type "build" from the command line and the build.bat batch file will be run. For Unix/Linux-based systems you can place the command line in a simple shell script to achieve similar build "automation."

Using Ant to Compile the Application

Manually compiling Java applications from the command line can be error prone and tedious. A popular alternative solution for compiling Java code is to use Apache Ant (http://ant.apache.org/). Ant is a Java-based build tool driven by an XML-based build file. The build file includes specific targets that correspond to steps in the build process, such as compiling classes, creating the distribution (such as a .jar file or .war file), and deploying the application. A full explanation of Ant is outside the scope of this book; however, a sample ant build file, build.xml, is given next for compiling the Mini HR application.

<project name="MiniHR" default="compile" basedir=".">   <property name="src.dir" location="src"/>   <property name="classes.dir" location="classes"/>   <property name="struts.lib.dir"         location="c:/java/struts-1.3.5/lib"/>   <property name="tomcat.lib.dir"         location="c:/java/apache-tomcat-5.5.17/common/lib"/>       <target name="compile">     <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="on">       <classpath>         <pathelement path="${classpath}"/>         <pathelement location="${tomcat.lib.dir}/servlet-api.jar"/>         <fileset dir="${struts.lib.dir}">           <include name="**/*.jar"/>         </fileset>       </classpath>     </javac>   </target> </project>

To run the Ant build file, simply type "ant" from the directory where your build.xml file is located. By default, Ant looks for a file named build.xml and runs it.

Packaging the Application

Because Struts applications are standard Java EE Web applications, this application will be packaged using the standard Web Archive format. Packaging the application as a .war file allows the application to be easily deployed on any Java EE–compliant servlet container with ease. Because you arranged the files for the Mini HR application in the standard Web Archive directory structure, packaging them into a .war file is straightforward.

Following is the command line for creating a MiniHR.war file, assuming that you run the command from the Mini HR application directory (c:\java\MiniHR):

jar cf MiniHR.war *

After you run the command, a MiniHR.war file will be created and ready for deployment.

Running the Application

Once you have packaged your application, running it is as simple as placing the Web Archive file into Tomcat's webapps directory and then starting up Tomcat. By default, Tomcat starts up on port 8080, and thus the server can be accessed at http://localhost:8080/. To access the Mini HR application, point your browser to http://localhost:8080/MiniHR/. You'll notice that the name of your Web Archive file is used for the URL of your application. Because you packaged the Mini HR application in a file called MiniHR.war, /MiniHR/ is used for the application's URL.

When you first access the http://localhost:8080/MiniHR/ URL, index.jsp will be run, because it was specified as the Welcome File in the web.xml deployment descriptor. From the opening page, select the Search for Employees link. The Search page allows you to search for employees by name or social security number. If you do not enter any search criteria, an error message will be shown on the page. Similarly, if you enter an invalid social security number, an error message will be shown on the page after you click the Search button.

Figures 2-2 through 2-5 show Mini HR in action.

image from book
Figure 2-2: The opening screen

image from book
Figure 2-3: The Employee Search screen

image from book
Figure 2-4: The Employee Search screen with a validation error

image from book
Figure 2-5: The Employee Search screen with search results



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2004
Pages: 165
Authors: James Holmes

Similar book on Amazon
Struts 2 in Action
Struts 2 in Action
Murach's Java Servlets and JSP, 2nd Edition
Murach's Java Servlets and JSP, 2nd Edition
Jakarta Struts For Dummies
Jakarta Struts For Dummies
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam

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