You are on page 1of 52

Sree

Table of contents
Introduction Classloading
Setup SSL
First webapp Monitoring and
Deployer Management
Manager Virtual Hosting
Realm Windows Service
Security Manager
JNDI Resources
JDBC DataSources
Introduction
The Apache Tomcat server is an open source, Java-
based web application container that was created to
run servlet and JavaServer Pages (JSP) web
applications. It was created under the Apache
subproject; however, due to its popularity, it is now
hosted as a separate Apache project, where it is
supported and enhanced by a group of volunteers from
the open source Java community.
Apache Tomcat is very stable and has all of the features
of a commercial web application container yet comes
under Open Source Apache License.
Tomcat versions and Supported API and JDK Versions
Apache Tomcat Servlet API JSPs API JDK
7.0 3.0 2.2 1.6 and 1.7
6.0 2.5 2.1 1.5
5.5 2.4 2.0 1.4
4.1 2.3 1.2 1.3
3.0 2.2 1.1 1.1
Prerequisites
Java 1.5 or later
Open the command prompt type java press enter. You
get this error java is not recognized as an internal or
external command, operable program or batch file.
You do not have java installation.
If you are not get this error java is installed.
Again type javac in command prompt and press enter.
You get this error javac is not recognized as an
internal or external command, operable program or
batch file. You did not set JAVA_HOME and path.
How to add environment variables
Click on windows Right click on My Computer or
Computer Properties Advanced System Settings
Environment Variables Under system variables
click on New Variable Name = JAVA_HOME and
Variable Value = C:\Program Files\Java\jdk1.6.0_22
click on Ok.
Check for path in system variables and select it click
on edit go to end. If semicolon not there put semicolon
and add the %JAVA_HOME%\bin.
Setup Environment
I will assume everyone will be using Windows.
Also make sure you have the Java SDK installed on
your PC.
The SDK includes the java compiler and some other
tools as well as the runtime environment.
You need the compiler to run tomcat.
Downloading Tomcat
Go to the Jakarta
binaries web site:
http://tomcat.apache.
org/download-70.cgi
Click the link for 32-
bit Windows zip.
Save Binary Into Disk and Extract
You should have
apache-tomcat-
7.0.xx.zip as a zip
icon on your
download location.
Right click and
choose
Extract Here.
This will create a
apache-tomcat-7.0.xx
folder also on your
download location.
Tomcat Directory Structure

Tomcat Base

bin conf lib logs temp webapps work

Servers.xml Host webapp


docs Root Manager webapp1
Manager 2
Tomcat-
users.xml
WEB-INF

lib classes Web.xml


Continue
Directory Contents

bin Contains the startup and shutdown scripts for both Windows and Linux.
Jar files with classes required for tomcat to start are also stored here.
conf Contains the main configuration files for Tomcat. The two most
important are server.xml and the global web.xml.
lib Contains the Tomcat Java Archive (jar) files, shared across all Tomcat
components. All web applications deployed to Tomcat can access the
libraries stored here. This includes the Servlet API and JSP API libraries.
logs Contains Tomcats log files.
temp Temporary file system storage.
webapps The directory where all web applications are deployed, and where you
place your WAR file when it is ready for deployment.
work Tomcats working directory where Tomcat places all servlets that are
generated from JSPs. If you want to see exactly how a particular JSP is
interpreted, look in this directory.
Running Tomcat
In the Tomcat folder, open the bin
folder.
Click the startup.bat icon.
You should see a black and white Java
command window.
You should not see any obvious java
error messages.
Open your browser and point to
http://localhost:8080.
You should see the Tomcat welcome
page.
Note startup.bat actually calls other
scripts in the same directory
(catalina.bat, particularly).
The .sh files are for running Tomcat
on Linux/Unix
Maybe Mac also.
Setting JAVA_HOME on Windows XP
From Start at the bottom left
of your screen, open the
control panel.
Select System to edit System
properties and choose the
Advanced tab.
Click the Environment
Variables Button.
Edit or add the JAVA_HOME
variable
It should point to the top
folder of your Java installation.
C:\Program Files
(x86)\Java\jdk1.x.0_xx, for
example.
Check My Computer to get
the actual name.
First webapp
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException{
message = "Hello World";
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}
public void destroy(){
}
}
Web.xml
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
Deployer
Introduction
Installation
A word on Contexts
Deployment on Tomcat startup
Deployment on running Tomcat server
Deploying using the Tomcat Manager
Deploying using the Client Deployer Package
Manager
Introduction
Configuring Manager Application Access
Supported Manager Commands
Deploy A New Application Remotely
Deploy A New Application from a Local Path
List Currently Deployed Applications
Reload An Existing Application
List OS and JVM Properties
List Available Global JNDI Resources
Start, stop and Undeploy an Existing Application
Executing Manager Commands With Ant
Supported Manager Commands
Deploy A New Application Remotely
Deploy A New Application from a LocalPath
List Currently Deployed Applications
Reload An Existing Application
List OS and JVM Properties
List Available Global JNDI Resource
Session Staticstics
Start an Existing Application
Stop an Existing Application
Undeploy an Existing Application
Finding memory leaks
Server Status
Realm Configuration
Overview
What is a Relam?
Configuring a Relam
Common Features
Digested Passwords
Example Application
Manager Application
Relam Logging
Standard Relam Implemations
Standard Relam Implementations
JDBCRelam
DataSourceRelam
JNDIRelam
UserDatabaseRelam
MemoryRelam
JASSRelam
CommbinedRelam
LockOutRelam
Security Manager
Permissions
Configuring Tomcat With A Security Manager
Configuring Package Protection in Tomcat
Troubleshooting
JNDI Resources
Web.xml configuration
Context.xml configuration
Global configuration
Using resources
Tomcat Standard Resource Factories
Generic JavaBean Resources
UserDatabase Resources
JavaMail Sessions
JDBC Data Sources
Adding Custom Resource Factories
JDBC DataSources
Driver Manager, the service provider mechanism and
memory leaks
Database Connection pool(DBCP) configuration
Installation
Preventing database connection pool leaks
MySQL DBCP Example
Oracle 8i, 9i & 10g
Postgre SQL
Class Loader
Like many server applications, Tomcat installs a variety of class loaders (that is,
classes that implement java.lang.ClassLoader) to allow different portions of the
container, and the web applications running on the container, to have access to
different repositories of available classes and resources.
When Tomcat is started, it creates a set of class loaders that are organized into
the following parent-child relationships, where the parent class loader is above
the child class loader:
Bootstrap

System

Common

webapp1 webapp2
SSL
Introduction to SSL
SSL and Tomcat
Certificates
General Tips on Running SSL
Configuration
Prepare the certificate Keystore
Edit the Tomcat Configuration File
Installing a certificate from a certificate authority
Create a local Certificate Signing Request(CSR)
Importing the Certificate
Monitoring and Management
Enabling JMX Remote
Manage Tomcat with JMX remote Ant Tasks
JMXAccessorOpenTask JMX open connection task
JMXAccessorGetTask get attribute value Ant task
JMXAccessorSetTask set attribute value Ant task
JMXAccessorInvokeTask invoke Mbean opration Ant task
JMXAccessorQueryTask: query MBean Ant task
JMXAccessorCreateTask: remote create MBean Ant task
JMXAccessorUnregisterTask: remote unregister MBean Ant task
JMXAccessorCondition: express condition
JMXAccessorEqualsCondition: equals MBean Ant condition
Using the JMXProxyServlet
Virtual Hosting
To setup many websites hosted into single tomcat
All website domain DNS entries pointed to single IP
where tomcat is placed.
We are going to host 3 sites in same tomcat server
www.sree.com
www.blog.sree.com
www.allsree.com
Outline
Root
www.sree.com Sree site
Context1
webapps
Context2

www.blog.sree.com Tomcat Blog.sree site


Root
Context1
webapps

www.allsree.com
Server Context2

Root
Allsree site
Context1
webapps
Context2

192.168.1.2
Tomcat server.xml layout
<Server>
<Service name="Catalina">
<Engine name="Catalina">
<Host name="localhost">
</Host>
</Engine>
</Service>
</Server>
Add more Host Nodes
<Host name=www.sree.com"
appBase=sree_webapps"/>
<Host name=www.blog.sree.com"
appBase=blog_webapps"/>
<Host name=www.allsree.com"
appBase=allsree_webapps"/>
DNS Entries
Domain Name IP Address
www.sree.com 192.168.1.2
www.blog.sree.com 192.168.1.2
www.allsree.com 192.168.1.2
How its working
In that request message have Host header
Tomcat server check the HTTP request message and
match to <host> tag in server.xml
If matched then tomcat pointed to corresponding app
base specified in server.xml and process the request.
What is the issue in virtual host in Tomcat
All websites are located in single system
It may not possible in big organization have multiple
departments
Each departments want to host his sites in their system
Outline Structure
How we achieve virtual host concept
Place the apache httpd webserver.
Apache httpd webserver accept all request and
delegate to appropriate department tomcat server.
Outline Structure
How apache httpd server and tomcat server can
communicate?
Through AJP Protocol
When run tomcat it can bind
Shutdown port
http port
https port (optional)
ajp port

So we need one module (mod_jk) to communicate


between apache httpd and tomcat
Steps
Install the apache httpd server
Install JK Connector (mod_jk)
Configure the JK connectot
Apply virtual host concept in apache httpd server
Install Apache httpd web server
./configure --prefix=/usr/local/apache --enable-
rewrite=shared --enable-proxy=shared
make
sudo make install
Install mod_jk connector
cd native
./configure --with-
apxs=/usr/local/apache/bin/apxs

make
sudo make install
Running Two Tomcat Servers
Web services often are applied to allow two Tomcat
(or other) servers communicate
One does display, the other runs commands.
So to really test things out and to understand what
is going on, you should set up and run two web
servers.
Preferably on two different machines.
Installing a second server on the same host follows
all of the same steps as before, with one additional
step.
You must modify server.xml
Finding server.xml
The file server.xml has all of
the server configuration
information.
This is located in the folder
apache-tomcat-7.0.xx/conf.
You only need to edit it in two
places.
See next slide
Double click it to open it with
your favorite text editor.
Make a backup copy of
server.xml before you change
things.
Tomcat Ports
Tomcats default settings listen to
three ports: 8080, 8005, 8009.
8080 is the http port number.
8005 is the shutdown port.
You can contact this to shutdown Tomcat
from another process.
8009 is the AJP port for running
Tomcat behind an Apache server.
Not needed here, but port opened
Tomcat can use other ports
8443 for SSL connections
Commented out by default.
Requires some additional configuration
8082 is for proxy connections
Redirecting HTTP to other servers.
Commented out by default.
You dont have to edit these.
For reference, use 9090, 9005, and
9009.
Changing Ports
Only one server at a time can accept connections on ports
8080, 8005, and 8009.
If you want run a second Tomcat server, you must change
the values of these ports for the second server.
Just edit server.xml to change these ports.
Shutdown the server first.
Values dont matter
For Linux/Unix, values <1024 are owned by root processes so you
normally cant use these values.
Now restart the server. Point your browser at the new port
number to check.
http://localhost:9090 for example.
Editing server.xml
The following slides show the config settings that you
need to change the shutdown, http, and ajp ports.
You can freely change other parameters if you want.
Note of course you are taking advantage of your basic
XML knowledge.
Shutdown port
<!-- A "Server" is a singleton element that represents the entire JVM,
which may contain one or more "Service" instances. The Server
listens for a shutdown command on the indicated port.

Note: A "Server" is not itself a "Container", so you may not


define subcomponents such as "Valves" or "Loggers" at this level.
-->

<Server port="9005" shutdown="SHUTDOWN" debug="0">


HTTP Connector
<!-- Define a non-SSL Coyote HTTP/1.1 Connector on
port 8080 -->
<Connector port="9090"
maxThreads="150" minSpareThreads="25"
maxSpareThreads="75"
enableLookups="false" redirectPort="8443"
acceptCount="100"
debug="0" connectionTimeout="20000"
disableUploadTimeout="true" />
<!-- Note : To disable connection timeouts, set
connectionTimeout value to 0 -->
AJP Port
<!-- Define a Coyote/JK2 AJP 1.3 Connector on port 8009
-->
<Connector port="9009"
enableLookups="false"
redirectPort="8443" debug="0"
protocol="AJP/1.3" />
How to install tomcat service
The safest way to manually install the service is to use
the provided service.bat script. Administrator
privileges are required to run this script.
NOTE: On Windows Vista or any other operating
system with User Account Control (UAC) you must
either disable UAC or right-click on cmd.exe and select
"Run as administrator" in order to run this script.
service.bat install
Continue
If using tomcat7.exe, you need to use
the //IS// parameter.
tomcat7 //IS//Tomcat7 --DisplayName="Apache
Tomcat 7" --Install="D:\apache-tomcat-
7.0.40\bin\tomcat7.exe" --Jvm=auto --StartMode=jvm
--StopMode=jvm --
StartClass=org.apache.catalina.startup.Bootstrap --
StartParams=start --
StopClass=org.apache.catalina.startup.Bootstrap --
StopParams=stop
Updating and removing service
tomcat7 //US//Tomcat7 --Description="Apache
Tomcat Server - http://tomcat.apache.org/ " --
Startup=auto --
Classpath=%JAVA_HOME%\lib\tools.jar;%CATALINA
_HOME%\bin\bootstrap.jar
tomcat7 //DS//Tomcat7
Unix Daemon
sudo vi /etc/init.d/tomcat
export JAVA_HOME=/usr/local/java
case $1 in
start)
sh /usr/local/Tomcat/apache-tomcat-7.0.-52/bin/startup.sh
;;
stop)
sh /usr/local/Tomcat/apache-tomcat-7.0.-52/bin/shutdown.sh
;;
restart)
sh /usr/local/Tomcat/apache-tomcat-7.0.-52/bin/shutdown.sh
sh /usr/local/Tomcat/apache-tomcat-7.0.-52/bin/startup.sh
;;
esac
exit 0
sudo chmod 755 /etc/init.d/tomcat
sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat
sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat

You might also like