Tomcat

Apache Tomcat Manager Tutorial

The Apache Tomcat® software is an open source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies

1. The tools

  • Apache Tomcat 8

2. Introduction

In this tutorial we are going to learn how to use the Tomcat Manager to deploy and undeploy Tomcat applications in a running environment without shutdown the servlet container.
It is very useful to have the capability to deploy a new web application, or undeploy an existing one, without having to shut down and restart the entire servlet container. In addition, you can request an existing application to reload itself.

3. Prerequisites

  • JDK 8 Installed
  • Tomcat 8 installed and running

4. Launch Tomcat

Go to Tomcat Install bin directory.

1 Tomcat install directory
1 Tomcat install directory

Type the command

start tomcat

C:\Java\Apache Tomcat 8.0.27\bin>startup.bat

A new window opens and you get the following output

startup output

Using CATALINA_BASE:   "C:\Java\Apache Tomcat 8.0.27"
Using CATALINA_HOME:   "C:\Java\Apache Tomcat 8.0.27"
Using CATALINA_TMPDIR: "C:\Java\Apache Tomcat 8.0.27\temp"
Using JRE_HOME:        "C:\Java\jdk1.8.0_40"
Using CLASSPATH:       "C:\Java\Apache Tomcat 8.0.27\bin\bootstrap.jar;C:\Java\Apache Tomcat 8.0.27\bin\tomcat-juli.jar"

And in the window opened by the script you get last lines like that:

Tomcat console

INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
INFO [main] org.apache.catalina.startup.Catalina.startServer startup in 2649 ms

Indicating that Tomcat has started. Check that Tomcat is started opening the link

You get the following output on the browser:

2 Tomcat welcome page
2 Tomcat welcome page

5. Tomcat manager User

By default, no user is included in the “manager-gui” role required to operate the “/manager/html” web application. To use the Tomcat Manager, you must define such a user

We are going to define a user to use the Tomcat manager application.

Edit the file:
/conf

3-tomcat-users-file
3-tomcat-users-file

Add a user with the admin, admin-gui, manager-gui roles.

manager-gui user

<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">

			  <user username="admin" password="admin" roles="admin, admin-gui, manager-gui"/>
</tomcat-users>

6. Launch the manager

Open the URL:

And click on the button Manager App

4-launch-manager
4-launch-manager

You should be prompted for a user and a password. Write the user and password defined before and click OK.

7. Manager Application

You should see a screen similar to the following image.

 5 Tomcat Web Application Manager

5 Tomcat Web Application Manager

Here you can:

List your applications. The application could be deployed or stopped, if you undeploy the application, the application id is being deleted from this list.

The default applications that come with a Tomcat binary distribution are:

/
Is the root of Tomcat when you open the URL http://localhost:8080

6 Tomcat Root
6 Tomcat Root

/docs
The Tomcat documentation.

7 Tomcat Docs
7 Tomcat Docs

/examples
Some Tomcat examples.

8 Tomcat Examples
8 Tomcat Examples

/host-manager
Is the web application to manage virtual hosts in Tomcat.

9 Tomcat Host Manager
9 Tomcat Host Manager

/manager
Is the Tomcat manager.

10 Tomcat Manager
10 Tomcat Manager

You could delete these defaults applications on a production server and manage everything from the command line. If you manage your Tomcat from a remote machine the Tomcat Manager Application and the Tomcat Host Manager Application could be useful.

8. Create a test application

We are going to use NetBeans to create a Test application to show how to use the Tomcat Manager to deploy, start, stop and undeploy an application.
Open NetBeans and right click on the projects windows then select New Project

11 New Project
11 New Project

In the next window choose Web Application and then press next.

12 New Web Application
12 New Web Application

Write a name for your new application and then press next.

13 Name the web application
13 Name the web application

From the next window choose Tomcat server and write a name for your context path. The context path is the relative path in the URL where you can run your application.

14 Context Path
14 Context Path

Now edit the file index.html and write some modifications as you like to show them when the application is running.
This file is going to be called by Tomcat when we run the application.

index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Tomcat Manager Tutorial</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>Tomcat Manager Tutorial</div>
    </body>
</html>

9. Create a deployable war file for our application

A WAR file (or Web application ARchive) is a JAR file used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages (HTML and related files) and other resources that together constitute a web application.

Right click on the project and press Build

 15 Create War File
15 Create War File

You should get a following similar output in the console:

Build WAR output

ant -f C:\\devel\\Java\\JEE\\TomcatManagerTutorial -Dnb.internal.action.name=build -DforceRedeploy=false -Dbrowser.context=C:\\devel\\Java\\JEE\\TomcatManagerTutorial dist
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
Created dir: C:\devel\Java\JEE\TomcatManagerTutorial\build\web\WEB-INF\classes
Created dir: C:\devel\Java\JEE\TomcatManagerTutorial\build\web\META-INF
Copying 1 file to C:\devel\Java\JEE\TomcatManagerTutorial\build\web\META-INF
Copying 2 files to C:\devel\Java\JEE\TomcatManagerTutorial\build\web
library-inclusion-in-archive:
library-inclusion-in-manifest:
Created dir: C:\devel\Java\JEE\TomcatManagerTutorial\build\empty
Created dir: C:\devel\Java\JEE\TomcatManagerTutorial\build\generated-sources\ap-source-output
compile:
compile-jsps:
Created dir: C:\devel\Java\JEE\TomcatManagerTutorial\dist
Building jar: C:\devel\Java\JEE\TomcatManagerTutorial\dist\TomcatManagerTutorial.war
do-dist:
dist:
BUILD SUCCESSFUL (total time: 0 seconds)

In the last line you could see BUILD SUCCESSFUL (total time: 0 seconds), indicating that everything wen OK.
The WAR file is created inside the dist folder in your project folder.

10. Deploy the WAR file

Open the Tomcat Manager, scroll down to the Deploy section and press Examine...

16 Examine War File
16 Examine War File

Locate the WAR file, select it and click open.

17 War File Location
17 War File Location

Once the file is selected, click on the button Deploy.

Now you can see your application on the list of Tomcat manager applications

 18 Tomcat manager applications list
18 Tomcat manager applications list

You can use these buttons to stop, reload and undeploy your application. When the application is stopped you can use the start button to start the application again.

Open the URL:

and you can see your application working now.

19 Application Running
19 Application Running

11. Conclusion

With the Tomcat Manager you can see and manage your applications running in the Tomcat Server, deploy new applications and undeploy the existing applications. With the web interface you can use your Tomcat Manager from any location with an Internet connection.
Just remember if you are going to make the Tomcat Manager accessible from the Internet, take a time to verify your security settings.

12. Download the Source Code

This was an tutorial of: Tomcat Manager.

Download
You can download the source files here: tomcatmanagertutorial

Jesus Boadas

I'm a self taught programmer, I began programming back in 1991 using an IBM A10 mainframe with Pascal an Assembler IBM 360/70 emulator and Turbo C on a X86 PC, since that I work for the banking industry with emerging technologies like Fox Pro, Visual Fox Pro, Visual Basic, Visual C++, Borland C++, lately I moved out to the Airline industry, leading designing and programming in-house web applications with Flex, Actionscript, PHP, Python and Rails and in the last 7 years I focused all my work in Java, working on Linux servers using GlassFish, TomCat, Apache and MySql.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button