Apache Tomcat is a standards compliant HTTP server and J2EE container for servlets and Java Server Pages. It is available for both Windows and Linux platforms and has very minimal dependencies on other system components. We will discuss about how to install and configure Tomcat as a portable application under Windows.
Step 1: Install Java
Tomcat is a pure Java application and requires a recent version of the JRE to function. The first step is to download a copy of the JRE and install the same on your local machine.
The recommended Java version for this particular article is 7.x for 32 bit platforms (x86). It is assumed that after the installation is complete, the JRE is available under the path:
C:\Program Files (x86)\Java\jre7
Step 2: Download Tomcat
Download a copy of Apache Tomcat. It is recommended that you download version 8.x of the product. Since the underlying platform is Windows, it is also recommended that you download the binary package ending with .zip extension.
Step 3: Install Tomcat
Use an appropriate decompression utility to extract the contents of the binary distribution into a suitable folder. Recent version of Microsoft Windows has support for browsing and extracting zip file contents from within the file explorer itself. Alternatively, you can use a free tool like 7zip to extract the zip file.
It is assumed that after extraction, the Tomcat server is available under the path: C:\Apache-Tomcat
Step 4: Setting up Java with Tomcat
The usual practice is to launch the Tomcat server through a batch script executed from the command prompt. This script file is named startup.bat for Windows platforms. For our installation, this file is located under the path C:\Apache-Tomcat\bin.
Before you launch the server, you need to open the startup.bat file in Notepad or an equivalent text editor. Then after the line that reads:
if “%OS%” == “Windows_NT” setlocal
Add a new line that says:
set “JAVA_HOME=C:\Program Files (x86)\Java\jre7”
Any other path is also valid as long as it points to a folder that contains a complete Java runtime distribution. Save changes made and exit the editor. You have now setup Tomcat to run with an appropriate JRE.
Apache Tomcat provides a large number of parameters and mechanisms using where almost every aspect of the server can be modified to suit end-business needs. For our installation, all required configuration files are located within the folder C:\Apache-Tomcat\conf. The following sections explain how to perform some of the basic basic customizations using these files.
- Running the Server on Default HTTP Port
The default port for running HTTP services is universally accepted to be 80. Out-of-the-box, Tomcat is configured to run on port 8080. To change this behavior, first close all running Tomcat instances. Then open the file C:\Apache-Tomcat\conf\server.xml in a suitable text editor to make the following changes:
Save the changes and launch the script:C:\Apache-Tomcat\bin\startup.bat from a command prompt. The Tomcat server should now start up on port 80. To ensure that everything is working as expected, open a web browser and point to the URL http://localhost. The default Tomcat home page should get loaded and displayed in the browser.
- Deny Access Over Any Other Protocol and/or Port.
The default configuration with which Tomcat is shipped allows for communication over both HTTP (port 80) and AJP (port 8009). The AJP protocol is specific for extremely fast communication between Apache Web Server and Tomcat. In our deployment scenario, there is no need for a separate Apache Web Server installation since the Tomcat also acts as a HTTP Server,.
To turn off the AJP protocol in such cases, open the file C:\Apache-Tomcat\conf\server.xml in a suitable text editor. Then completely comment out the following line:
Reduce the Amount of Output Logs Generated.
After you start Tomcat, you will notice that quite a bit of log information is printed on the console by the running server. Additional log files are also generated under the path C:\Apache-Tomcat\logs. For production deployment, you would want only logs corresponding to warnings and errors to be generated and written to log files instead of the command window.
The file that defines the logging behaviour of Tomcat is named logging.properties and is located under the Tomcat configuration folder (in our case, C:\Apache-Tomcat\conf). Open this file using a text editor and make the following changes:
Completely comment out the configuration related to Console handler as no logs are to be written to the command window.
Display a custom page instead of the default Tomcat Home Page.
The end-goal of this customization is to have the URL http://localhost to display a custom page instead of the default Tomcat server home page. This custom page simply displays the following text:
Mon Dec 03 15:17:24 CST 2012
The second line prints out the current date and time when the page is displayed.
Apache Tomcat is capable of hosting multiple web applications on the same server instance. Each web application must follow the structure and content for such applications as prescribed by the J2EE specification. For our installation, each such web application must be placed in a separate subfolder under the path C:\Apache-Tomcat\webapps.
The name of the sub-folder is used as the extra path information when accessing using a Web browser. So for example, a Web application that is placed under the path C:\Apache-Tomcat\webapps\test is accessible via the URL http://localhost/test. The web application corresponding to http://localhost/ is always placed in the special folder named C:\Apache-Tomcat\webapps\ROOT.
To modify the default server home page, replace the contents of the file C:\Apache-Tomcat\webapps\ROOT\index.jsp with the following:
<title> Your title here </title>
Save the file. Then reload the URL http://localhost/ in the Web browser to see the changes without having to restart the Tomcat server.