Apache Tomcat – Tutorial

Version 0.5

 Copyright © 2008 – 2010 Lars Vogel

03.08.2010

Revision History
Revision 0.1 18.11.2008 Lars Vogel
Created
Revision 0.2-0.5 09.04.2009 – 03.08.2010 Lars Vogel
bug fixed and enhancements
Apache Tomcat

This article describes the installation and usage of Apache Tomcat for Java web development. Tomcat version 6.x is used in this tutorial.


1. Apache Tomcat

Apache Tomcat is a webcontainer which allows to run servlet and JavaServer Pages based web applications. Most of the modern Java web frameworks are based on servlets and JavaServer Pages and can run on Apache Tomcat, e.g. JavaServer Faces, Struts, Spring.

Apache Tomcat also provides per default a HTTP connector on port 8080, e.g. Tomcat can also be used as HTTP server even though the performance of Tomcat is not as good as the performance of the Apache HTTP server.

2. Installation

2.1. Windows

Download the Windows installer for Tomcat6 from Apache Tomcat Homepage and run the installer.

The installation directory of Tomcat looks like the following:

2.2. Ubuntu Linux

For Ubuntu run via sudo the following commands.

				
apt-get install tomcat6
apt-get install tomcat6-admin
apt-get install tomcat6-docs
apt-get install tomcat6-examples

2.3. Admin user

The default user under windows for tomcat is “admin” with the password “admin”. The maintained users can be found in the tomcat installation directory /conf/tomcat-users.xml

On Ubuntu the admin user is usually not created automatically, you have to add the user entry manually to /etc/tomcat6/tomcat-users.xml. Check http://localhost:8080/manager/html for the required user in the error message.

3. Managing Apache Tomcat

3.1. Start Tomcat on Windows

To start Tomcat use “tomcat6.exe” in the bin directory. Alternatively you can use a graphical userinterface to start Tomcat. From your menu select “Configure Tomcat”.

To start Tomcat directly press the button “Start”. Tomcat can get configured to started automatically via the “Automatic” selection in the drop-down “Startup type”.

3.2. Start Tomcat on Ubuntu (Linux)

In Ubuntu Tomcat is automatically started. To restart tomcat use the command sudo /etc/init.d/tomcat6 restart

3.3. Test Tomcat

The default port for Tomcat is 8080. After starting Tomcat on your local you can validate if Tomcat is running the the URL: http://localhost:8080.

3.4. Deployment

The standard deployment format for webapplications is .war. If you have create a war application just put this application into the “webapps” folder. The next time tomcat starts it will unpack the war and make the application available.

Web applications may require external libraries.Typically web application contain there own libraries but if you want to make certain libraries avaiable for all applications you can put them into the folder “lib” and a subfolder below “lib”. These libraries are then available for all web applications.

4. Security Manager

Tomcat uses the same security model as Java. The default security manager configured in Tomcat will for example prevent the webapplication to write to the file system. This security manager is configured in the catalina.policy file in the conf directory.

For details on the manager see http://tomcat.apache.org/tomcat-6.0-doc/security-manager-howto.html . If you want for example to grand the webapplication “performance” access to all files in the server directory c:\temp\wstest then add the following entry to “catalina.policy”.

			
grant codeBase "file:${catalina.home}/webapps/performance/-"  {
java.io.FilePermission "c:\temp\wstest\<<ALL FILES>>", "read,write,execute,delete";
};

5. Developing Java web applications

After going through the setup you properly want to learn how to develop servlets and JSP on Tomcat. Please see Servlets and JSP development – Tutorial

6. Tomcat as HTTP Server

Apache Tomcat contains also a HTTP connector which can be used to serve static HTML pages. The standard directory which will be served by Apache Tomcat is below the Tomcat installation directory “webapps/ROOT”. Place static content which should be served by Tomcat into this directory.

Tip

To allow directory browsing via Apache Tomcat change the parameter “listings” in the file conf/web.xml from false to true.

					 
 <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s