You can download Tomcat from the Jakarta Project’s Tomcat download page. At this writing, the “production” version is Tomcat 3.2.3. The binary distribution (the download file) is a 3.1MB zip file named jakarta-tomcat-3.2.3.zip. There is no installation program?you just unzip the file. You can unzip the file anywhere, but I recommend that you create a directory dedicated to Tomcat. When you unzip the jakarta-tomcat-3.2.3.zip file, you will see the directories (see Figure 1).
|bin||You’ll find a number of batch files here. This is where you start/stop the server using startup.bat and shutdown.bat. Some key environmental variables are also defined here. Finally, if you want to precompile your JSP pages, the jspc.bat file can simplify the process.|
|conf||This directory contains all the configuration files for the server. The main configuration files are in XML. You may want to glance through the XML files to familiarize yourself with their content. The security permissions are also here in the file tomcat.policy.|
|doc||You will find various documents in this directory such as the User’s Guide, Tomcat security, and application development. The API documentation for Java Servlets is available as a separate download from Apache web site.|
|lib||Several important Jar files are located here including servlet.jar, jaxp.jar, and webserver.jar. Tomcat relies on XML parsers because most of its configuration files are in XML. These jar files are added to your CLASSPATH when you start the Tomcat server.|
|logs||Tomcat puts its log files in this directory. Servlet invocations are stored in servlet.log and JSP pages are logged in jasper.log. These files can be very helpful when debugging your applications.|
|src||Source code for the server.|
|webapps||Your application files are stored under this directory. I’ll discuss the structure and the mechanics later in this article. As an application developer, you’ll spend most of your time working in the webapps directory.|
|work||Tomcat produces a number of temporary/working files, which are stored in this directory. For example, a JSP page first needs to be converted into a Servlet and then compiled. The Servlet source code and the class file produced by the compiler are stored in this directory. You should not delete items from this directory while the server is working. Sometimes it is helpful to look at the translated Servlet source code for debugging purposes.|
The logs and work directories will not be there initially after you unzip the distribution file, but Tomcat creates them automatically the very first time you start it.
Step 2: Start the Server
After unzipping the file, navigate to the “bin” directory and start Tomcat by running the startup.bat file. You must have the JDK installed before you can start Tomcat. The JDK contains the Java runtime engine. The environment variable JAVA_HOME tells Tomcat where to find your JDK. You can set this variable from the Control Panel or by modifying the file tomcat.bat in the bin directory. Simply add a line at the beginning of the file to point to your JDK installation. For example, if you installed the JDK in the c:jdk1.2 directory, you would write:
Step 3: Create Application Directories
You are now ready to deploy your Web applications onto Tomcat. Several sample applications ship with the Tomcat binary distribution. If you don’t already have an application to deploy, you can use one of the sample applications for practice. The deployment process can be complicated depending on your application.
A typical Web application consists of HTML pages, images, JSPs, Servlets, Beans, and configuration files. Tomcat requires you to package your application in a special way, but all your application’s files go into a subdirectory of the webapps directory.
First, create a directory for your application, for example: webappsfoo. Next, create a subdirectory called WEB-INF and place your deployment descriptor file there. A deployment descriptor is an XML file called web.xml that contains information about your application. The file is secure because Tomcat will not serve files contained in the WEB-INF directory directly to clients. You can easily copy and modify one of the existing sample web.xml files to customize it for your application. Next, create a subdirectory under the WEB-INF folder called classes. Place any Servlets or utility classes your application may use in the classes folder. For example, if you create an application called foo, the directory hierarchy for an application called foo becomes:
foo fooWEB-INFweb.xml fooWEB-INFclasses
Step 4: Deploy Your Application
Each Web application must have its own ServletContext. The ServletContext acts as a link between your application and the Tomcat engine. You create a ServletContext by adding a line in the conf/server.xml file. It looks like this:
The path attribute tells Tomcat that all request URLs ending in /foo belong to the foo application. The docBase attribute tells Tomcat that the foo application resides in a subdirectory of the webapps folder called foo.
You can now copy all the JSP, HTML, and image files associated with your application. You place JSP and HTML files in the application directory. Next, create a web.xml file in the WEB-INF folder (you can copy and modify one of the existing web.xml files to get started). Copy your Servlets to the /foo/WEB-INF/classes directory and add an entry to the web.xml file for each Servlet associated with the application. The Servlet entry provides Tomcat with information about the Servlet, its class file, and any parameters that may need to be passed to the Servlet. If your application consists only of JSP pages, then you don’t need to worry about modifying the web.xml file.
jar cvf foo.war