This
article will show us how to use Apache Tomcat as beginners. I illustrate on
Window 7 OS.
Download
When
I write this article, the current version is 7.0. After you download the zip
file, you extract to a location on your computer. Assume that we extract it to C:\Tomcat.
You also should set environment variables for Tomcat, for example: TOMCAT_HOME
= C:\Tomcat and add TOMCAT_HOME \bin to the PATH variable.
Configuration Users and Roles
First
of all, we have to declare users details for Tomcat. We also need to configure
the access roles for users. Tomcat has various roles for users. To add or
modify users’ information, we open TOMCAT_HOME\conf\tomcat-users.xml
to do it. From the Tomcat zip file, this file is empty with no user.
Users
are declared with a <user> tag which
have 3 attributes: username, password, and roles (could have many roles
separated by a comma). We put <user>
tags inside the <tomcat-users> tag.
<?xml
version='1.0' encoding='utf-8'?>
<tomcat-users>
<user username="admin"
password="admin" roles="admin,manager,manager-gui"/>
<user username="ducky"
password="2013" roles="manager-gui"/>
</tomcat-users>
As
examples above, user admin has password
admin and 3 roles: admin, manager, and manager-gui.
User ducky has password 2013 and two roles: manage-gui and admin-gui.
Note
that, by default no user is included in the ‘manager-gui’ role required to operate the "/manager/html"
web application (TOMCAT description). The ‘manager-gui’
allows users using the web application. The ‘admin-gui’ allows users checking and changing hosting (or the host
manager role).
Configuration Server Ports and Host
To configure these information, we open file .
The default port of Tomcat is
8080. If you install it on your computer, you could connect by
address:
http://localhost:8080. To change
connection port of Tomcat on web browsers (HTTP protocol), for instance, to
8081, we change as below:
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000"
redirectPort="8443" />
Start and Stop Tomcat
To start Tomcat, you could run TOMCAT_HOME\bin\startup.bat.
If you open Tomcat by browsers, you could see the GUI like this and configure
and see Server status, applications, hosting.
To shutdow Tomcat, you could run TOMCAT_HOME\bin\shutdow.bat
Deploy applications on Tomcat
To deploy applications by war files, you start up Tomcat,
then browse the manage applications by address
http://localhost:8080/manager/html.
Scroll down to the Deploy part, you could choose a war file and deploy it.
After deployed, the application will appear on the Application list.
To deploy applications by html files, for instance, you
could add the application by copy and past directly to the folder: TOMCAT_HOME\webapps. I create a folder Test (it is also the Test application),
and put a simple index.html inside Test:
<html>
<body>
This is demo for applications
</body>
<html>
Then,
we start Tomcat, and we could see from the Application list on application
manager, the test application appears as others. Click on the link, the content
of index.html of test application will be displayed.
Note:
After we deploy applications (by wars or directly), we must restart Tomcat if
it is running. Tomcat just recognizes applications on the webapps folder since the last time it starts.