Deployment

Definition

Adding a web application to Tomcat

A web application maps request URLs to zero to many Filters and exactly one Servlet

Plus a supporting cast of static content, security configuration, resources, session management and more

Web application (Servlet spec) == Context (Tomcat)

Web application structure

Directory or Web ARchive (WAR)

WAR has same format as JAR (and zip)

Web application structure

/ contains static content (and JSPs)

/WEB-INF/ may contain web.xml file

/META-INF/ may contain context.xml file

/WEB-INF/classes Java class files

/WEB-INF/lib JAR files

Servlet

Java class

Accepts an HTTP request

Generates an HTTP response

Default Servlet: static files

JSP Servlet (Jasper): JSP files

Mapping rules

Web applications are deployed to a context path

First mapping rule is map to longest matching context path

Context Paths

The context path is always derived from the WAR, DIR or context.xml

Any Servlet 4 defined preference in web.xml is ignored

Context Paths

Context Path Context Name Base File Name Example File Names (.xml, .war & directory)
/foo /foo foo foo.xml, foo.war, foo
/foo/bar /foo/bar foo#bar foo#bar.xml, foo#bar.war, foo#bar
Empty String Empty String ROOT ROOT.xml, ROOT.war, ROOT

Automatic deployment

Default Host appBase is $CATALINA_BASE/webapps

Directories and WARs placed here are deployed at start-up by default

WAR or directory for a Context is called the docBase

deployOnStartUp, autoDeploy, unpackWARs

Deployment Descriptors

context.xml files

/META-INF/context.xml

$CATALINA_BASE/conf/engine-name/host-name/base-name.xml

$CATALINA_BASE/conf/Catalina/localhost/manager.xml

$CATALINA_BASE/conf/engine-name/host-name/ is known as xmlBase

Deployment Descriptors

copyXML - copy to $CATALINA_BASE

deployXML - ignore /META-INF/context.xml

Note: defaults changed in Tomcat 7

Can point to a docBase outside the appBase

Automatic deployment

1st Deployment descriptors in xmlBase

2nd WARs in appBase

3rd Directories in appBase

Automatic deployment

Detecting changes

Tomcat will pick up the change

Aims to do the minimum necessary for the change to take effect

The details are documented

Automatic deployment

You can define Contexts in server.xml

Don't

Changes require a Tomcat restart

Changes while shutdown

Most will take effect on start-up

Updated WAR with unpacked DIR used to be ignored

/META-INF/war-tracker as of 8.0.21

Parallel deployment

Different versions of web application at the same time

Sessions using old version continue to do so

New sessions use latest version

Parallel deployment

foo##001.war

foo##002.war

Version is a string, not a number

Demonstration

Automatic deployment

Changes

Parallel deployment

Exercise

Create a web application and deploy it

Demonstrate parallel deployment