devxlogo

A Quick Guide to Installing Tomcat on Windows

A Quick Guide to Installing Tomcat on Windows

akarta Tomcat, an Open Source Servlet and JSP container, provides a simple (and free) environment for developing and testing Web applications. Tomcat is the reference implementation for the Servlet API and JSP. It’s not a high-performance Servlet engine, but it is a complete implementation of the Servlet API.

For developers accustomed to GUI-driven Windows applications, installing Tomcat can be frustrating. In this article, I’ll give you a step-by-step procedure to install and configure Tomcat on the Windows platform.

Java Servlets provide a framework for creating Web applications. Servlets are Java classes that use the Servlet API to accept a Request object from the browser and produce a Response object. For those of you familiar with Microsoft’s Active Server Pages (ASP) technology, Java Server Pages (JSP) are similar but they use compiled Java Servlets rather than an interpreted scripting language. Like ASP pages, JSP pages consist of code mixed with HTML. The JSP hosting engine, commonly referred to as a “JSP container,” converts the pages to a Servlet, then compiles and executes them.

The Tomcat engine is a Java program. Before you can run Tomcat, you must have the Java Development Kit (JDK) version 1.2 or later installed on your computer.
Step 1: Install Tomcat
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).



DIRECTORYDESCRIPTION
binYou’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.
confThis 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.
docYou 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.
libSeveral 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.
logsTomcat 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.
srcSource code for the server.
webappsYour 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.
workTomcat 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:

   SET JAVA_HOME=c:jdk1.2
The other important environment variable is called CLASSPATH. The CLASSPATH variable helps the Java runtime find various classes needed by an application. Usually, these Java classes are grouped together in a single JAR (Java Archive) file. Tomcat has several such JAR files that include classes for XML parsing, Servlets, and the JSP compiler. You can find them in the “lib” directory. Tomcat automatically appends the location of its jar files to your existing CLASSPATH.

Warning: Sometimes the process of appending the jar file location to your CLASSPATH environment variable causes the variable size to exceed the maximum allowable size. The tomcat.bat file contains a description of how to make the appropriate modifications manually.

Another point to consider is that JSP pages must be compiled the very first time someone accesses the JSP page. This compilation process converts the JSP page into a Servlet. This requires access to the Java compiler. Tomcat will not be able to call the Java compiler if the directory path to the Java compiler includes spaces. To avoid the problem, install the JDK in a directory with a name that contains no spaces, such as JDK1.3.

Figure 2. Tomcat Introduction Page : You’ll see this page if Tomcat has successfully installed.

After starting Tomcat, open your browser and type the following URL: http://localhost:8080. Tomcat uses port 8080 by default, so make sure that port is not being used by another server, such as Internet Information Server (IIS). If there is a port conflict, you can change Tomcat’s default port by modifying the file server.xml in the conf directory. Search for the number 8080 in the file and change it to an unused port number. When Tomcat starts successfully, you will see an introduction page in your browser similar to Figure 2.

There are two links towards the middle of the page pointing to Servlet and JSP examples. You should click on a few examples to make sure everything is working correctly.

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.
Step 5: Create a Web Archive File
As a last step, create a Web Archive file (WAR). The WAR file is similar to a Windows CAB file?it packages your application. At a command prompt, switch to the directory you created under webapps for your application, for example c:Tomcatwebappsfoo, and then issue the following command:
   jar cvf foo.war
Copy the resulting foo.war file to the webapps directory of your Tomcat installation.

You’ve completed the steps to install Tomcat and configure a JSP application. Spend some time familiarizing yourself with Tomcat’s directory structure and configuration files, so that you can later focus on your application-specific needs. You can also learn a great deal by studying the sample applications that ship with Tomcat.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist