Tomcat & Cocoon
R. Alexander Milowski
milowski@sims.berkeley.edu
School of Information Management and Systems
#1
Tomcat 5.0.19
Tomcat is a J2EE servlet container.
It supports JSP 2.0 and Servlet 2.4
It is also a web server.
More information is available at: http://jakarta.apache.org/tomcat/index.html
You can download it at: http://apache.towardex.com/jakarta/tomcat-5/v5.0.19/bin/ (~ 12MB)
#2
Checking JAVA_HOME
You need a JDK 1.4.x installed to run Tomcat.
Also, the environment variable JAVA_HOME must be set correctly.
It should point to where Java 1.4.x is installed.
On Mac OS X & other unix-based systems:
set JAVA_HOME=/usr
On some unix systems, you need to point to where it is installed. For example:
set JAVA_HOME=/opt/j2sdk1.4.2
On windows, point to where you installed the JDK:
set JAVA_HOME=c:\Program Files\Java1.4\
#3
Getting Started
Unpack the tomcat distribution:
unzip jakarta-tomcat-5.0.19.zip
Make sure that 'JAVA_HOME' is set in your environment.
Open up a shell and start tomcat:
On Unix or Mac OS X:
cd jakarta-tomcat-5.0.19/bin ./startup.sh
On Windows
cd jakarta-tomcat-5.0.19/bin startup
Note: On Unix or Mac OS X you may need to make the scripts executable first:
cd jakarta-tomcat-5.0.19/bin chmod u+x *.sh
Check to see if it running at http://localhost:8080
#4
Stopping Tomcat
Open up a shell and start tomcat:
On Unix or Mac OS X:
cd jakarta-tomcat-5.0.19/bin ./shutdown.sh
On Windows
cd jakarta-tomcat-5.0.19/bin shutdown
#5
Setting Up an Administrator
Open this file in your favorite XML editor:
jakarta-tomcat-5.0.19/conf/tomcat-users.xml
Add these two elements after the last 'role' element:
<role rolename="manager"/> <role rolename="admin"/>
Add this element after the last 'user' element:
<user username="admin" password="admin" roles="admin,manager"/>
If you have tomcat running, you'll have to restart it.
#6
The Web Application Manager
Start tomcat.
On Unix or Mac OS X:
cd jakarta-tomcat-5.0.19/bin ./startup.sh
On Windows
cd jakarta-tomcat-5.0.19/bin startup
Go to this URL: http://localhost:8080/manager/html
Use your administrator username and password to login (e.g. 'admin' for both).
#7
What You Are Seeing
The 'Applications' list shows you all the WAR files installed:
'Path' is the relative URI of the application.
'Running' is the status of the application.
'Commands' will let you manipulate the application.
WARNING: The 'Undeploy' command will delete the application files (WAR contents) from your system. Be careful!
#8
Deploying a Small Application
You can upload small WAR files to install them:
Login to the manager at http://localhost:8080/manager/html
Choose the file to install in the section labeled 'WAR file to deploy'.
Submit the file with the 'Deploy' button directly below the file chooser.
You can the access your application at the relative URI using the base name of the WAR file.
For example, myapp.war becomes http://localhost:8080/myapp/
WARNING: You really don't want to do this with large WAR files.
#9
Deploying a Large Application
For large WAR files, do the following:
Place the WAR file on the host running tomcat (e.g. /Users/you/yourapp.war).
Login to the manager at http://localhost:8080/manager/html
Under 'Deploy ... WAR file located on Server, do the following:
In the 'WAR or Directory URL, set the location of the file as a URL:
file:/Users/you/yourapp.war
Click on the 'Deploy' button just below this text field to install the application.
The application may not show up immediately on the list of applications because it is still installing. To check the install, click on 'List Applications' and wait.
Your application should show up when the web browser returns the results of (4).
You can the access your application the same as in the previous slide.
#10
Accessing Applications the First Time
You may experience a delay the first time you access your application.
Tomcat may be compiling and optimizing your application.
Subsequent access times should be almost immediate.
#11
What's A WAR File?
WAR - Web Application Archive
It contains all the code, documents, etc. necessary to run your application.
It is structured for auto-deployment.
You build WAR files from your source code and implementation libraries (e.g. Cocoon).
#12
WAR File Structure
A war file is just a jar file (like a zip archive) with a standard structure.
Basic structure:
WEB-INF/ - directory for application support lib/ - supporting jar files web.xml - configuration file for the application ...
'WEB-INF' is used to package:
Configuration
Supporting compiled java code.
Other non-public information.
The 'web.xml' file tells the J2EE container (Tomcat) what to do.
Everything outside of 'WEB-INF' is public content available via the web server (Tomcat).
#13
web.xml
The configuration file for an application is an XML document.
For JSP pages, you can have an empty document:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"/>
You can also configure servlets:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"> <servlet> <servlet-name>MyApp</servlet-name> <servlet-class>edu.berkeley.cde.MyApp</servlet-class> <init-param> <param-name>test</param-name> <param-value>yes</param-value> </init-param> <init-param> <param-name>log</param-name> <param-value>fine</param-value> </init-param> </servlet> </web-app>
#14
JSP & web.xml
You can also configure JSP pages to act like servlets:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"> <servlet> <servlet-name>event.service</servlet-name> <jsp-file>/event.jsp</jsp-file> <init-param> <param-name>eventRepository</param-name> <param-value>/data/events/</param-value> </init-param> </servlet> </web-app>
#15
Multiple Applications
Each WAR file creates its own environment.
On the same server, you can have different versions of the same Java libraries in different applications.
In Tomcat 5, ROOT.war is automatically mapped to the root of your web server.
In Tomcat 5, ROOT.war is the only way to have something mapped to the root of your web server.
#16
Using Cocoon
Cocoon is a servlet-based application.
Just install a cocoon distribution into your WEB-INF directory of your WAR file:
Copy all the distribution files/directory into the WEB-INF directory.
Make sure that the 'sitemap' element points to your sitemap.xmap file in the WEB-INF directory:
<sitemap check-reload="yes" config="resource://org/apache/cocoon/components/treeprocessor/treeprocessor-builtins.xml" file="context://WEB-INF/sitemap.xmap" logger="sitemap"/>
Setup your sitemap.xmap file in WEB-INF (see Cocoon documentation).
Cocoon has many, many extra "kits" that provide optional features.
Everything is about 31MB.
I've trimmed this down to about 15MB.