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)
Directory or Web ARchive (WAR)
WAR has same format as JAR (and zip)
/ 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
Java class
Accepts an HTTP request
Generates an HTTP response
Default Servlet: static files
JSP Servlet (Jasper): JSP files
Web applications are deployed to a context path
First mapping rule is map to longest matching context path
The context path is always derived from the WAR, DIR or context.xml
Any Servlet 4 defined preference in web.xml is ignored
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 |
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
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
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
1st Deployment descriptors in xmlBase
2nd WARs in appBase
3rd Directories in appBase
Detecting changes
Tomcat will pick up the change
Aims to do the minimum necessary for the change to take effect
The details are documented
You can define Contexts in server.xml
Don't
Changes require a Tomcat restart
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
Different versions of web application at the same time
Sessions using old version continue to do so
New sessions use latest version
foo##001.war
foo##002.war
Version is a string, not a number
Automatic deployment
Changes
Parallel deployment
Create a web application and deploy it
Demonstrate parallel deployment