MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

Getting and running Tomcat

Tomcat is a small Web server written in Java. You can download Tomcat from http://tomcat.apache.org/. These notes use version 6.0, although any recent version should work. Tomcat will come as a compressed archive. Uncompress this into a directory where you would find it convenient to work from.

Before running Tomcat, you need to ensure that the JAVA_HOME environment variable is set. The JAVA_HOME variable should hold the directory where you copy of the Java development tools has been installed. You can set this by right clicking on “My Computer” and selecting “Properties”. Click on the “Advanced” tab and then on the “Environment Variables” button. Under “User variables”, click the “New” button. The name of the variable should be “JAVA_HOME” and the variable value should be the directory where Java is installed. For example, this could be: “C:\Program Files\Java\jdk1.5.0_03”.

Within the uncompressed Tomcat directory, you can find the files to start and stop the Tomcat server in the “bin” subdirectory. These are named startup.bat and shutdown.bat. After running startup.bat, you can test that Tomcat is working by visiting this ulink url your Web browser: http://localhost:8080/ If you can see a Web page, then you have installed it correctly.

What has happened here is that Tomcat is now running as a Web server. You can always access a Web server on your own machine using the URL http://localhost. By default, Tomcat is set up to listen for HTTP requests on port 8080, and hence you use the full address, http://localhost:8080/.

Creating a simple Web page

Tomcat allows you to create Web pages, similarly to how a normal Web server would. The only difference is that each Web page should be placed within a subdirectory of the webapps directory. Our first example is a plain HTML file: first create a new subdirectory in webapps called “first”. Inside this directory, create an html file called “index.html”. Add the following text to it:

	  <html>
	  <head>
	  <title>Hello!</title>
	  </head>
	  <body>
	  Hello, world!
	  </body>
	  </html>
	

Ensure that Tomcat is running by executing startup.bat. You can view this page by visiting http://localhost:8080/first/index.html. Web servers, when not given the name of an HTML file, typically look for one entitled index.html, and Tomcat does this as well. This means that you can also see this page by visiting http://localhost:8080/first.