Installing Struts

In this tutorial we'll see how to install Tomcat and how to bundle Struts along with your web application.

The first thing to note is that Struts works within a servlet container. Although you can use any conformant servlet container (conformant to Servlet API Specification, version 2.2/2.3) , we'll be using Tomcat . It's free, robust and ubiquitious.

If you already have a servlet container, you can skip the next section, although you might have to do some fiddling to get Struts to work.

If you're already using Tomcat, be sure to get 4.X or (better) 5.0.X, because higher versions (5.5.X and above) require JDK 5.0 and versions lower than 4.X require some fiddling to work with Struts.

Installing Tomcat

  1. You need to have a JDK installed (at least 1.2, 1.5 is best). Having a JRE is not enough - you need a JDK to allow Tomcat to compile your JSP pages. The base directory of this JDK is known as JAVA_HOME.
  2. Download Tomcat. I suggest version 5.0.X. Download the binaries (.zip) file.
  3. Unzip the Tomcat binaries to a convenient location. The batch files to start and stop Tomcat are in ./bin and you deploy your web apps in ./webapps (locations relative to where you unzipped Tomcat)
  4. Open up ./bin/startup.bat (or ./bin/startup.sh if you're using *NIX) and put in an extra line at the start to set up the JAVA_HOME path. Do the same for the shutdown batch file.

Fire up the startup batch file to launch Tomcat. You can see the Tomcat start page on localhost (try localhost:8080 if it doesn't work). To shut down Tomcat you use the shutdown batch file.

Development directories

During development it helps for your source directories to reflect the way the binaries of your webapp will be stored. Create the following directories under your main development directory:

src
holds your Java source files and batch files to compile the application and prepare it for deployment.
web
holds html, jsp and image files.
web/WEB-INF
The struts-config.xml file goes here, as does web.xml. The first describes how your application will be pieced together by Struts and the other is required by the Servlet standard. We won't be changing the web.xml file much and you can use the standard one distributed with Struts.
web/resources
Put the resources files (*.properties) into this directory. We'll be using just one properties file which we'll call Application.properties.
lib
holds the Struts and other jar files required to compile your application and Struts tag library files (*.tld).
deploy
temporary directory that holds the unpacked, compiled web application.

If some of the comments above appear cryptic at this stage, don't sweat it. We'll go through everything in detail later.

Installing Struts

Once you've successfully installed Tomcat and created your development directories, you're in a position to install Struts:

  1. Download Struts and save the zip file to somewhere handy. You cannot do a one-time install of Struts on Tomcat (or any other servlet container). Instead, you will have to bundle Struts along with each webapp you create.
  2. Extract the contents of the Struts zip file to a temporary folder. There should be a "lib" directory among the unzipped contents. Copy all ".jar" and all ".tld" files to your development "lib" directory.
  3. In your Tomcat installation, there should be a /common/lib subfolder. Within this folder is a file called "servlet-api.jar". Copy this file into your development "lib" directory. Strictly speaking, this step isn't part of a Struts install but the "servlet-api.jar" file contains the javax.servlet package, which you need in order to compile your Struts webapps.

That's it! If you're using Tomcat 4.X or 5.X , you can now compile, deploy and run Struts applications. If you're using earlier Tomcat versions or other servlet containers you might have some fiddling ahead of you.

In Summary

  1. Struts is a framework built on top of Java Servlets. This means you need a servlet container to run Struts webapps.
  2. Struts is always bundled along with your webapp.
  3. You'll need a copy of the javax.servlet package. This isn't bundled with Struts, but you can find it (at least for Tomcat- it's servlet-api.jar) with your servlet container distribution.