Deploy Web Application on Tomcat 4.1.12

by Jeff Hunter, Sr. Database Administrator


Create Web Application Directories

cd $CATALINA_HOME/webapps
mkdir webdba
mkdir webdba/images
mkdir webdba/WEB-INF
mkdir webdba/WEB-INF/classes
mkdir webdba/WEB-INF/lib


Add Minimal Deployment Descriptor File

Create a default deployment descriptor (web.xml) file that contains only the DTD, describing the web.xml file, and an empty element. You can simply copy the deployment descriptor file from the /examples application and take out the webapp details.

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
</web-app>
$CATALINA_HOME/webapps/webdba/WEB-INF/web.xml

Place the above web.xml file in the directory:

$CATALINA_HOME/webapps/webdba/WEB-INF


Enabling the Invoker Servlet

The invoker servlet lets you run servlets without first making changes to the WEB-INF/web.xml file in your Web Application. Instead, you just drop your servlet into WEB-INF/classes and use the URL http://localhost/servlet/ServletName. The invoker servlet is extremely convenient when learning and even during initial development. But, as discussed at length in many books, you do not want it on at deployment time.

Up until Apache Tomcat 4.1.12, the invoker was enabled by default. However, a security flaw was recently uncovered whereby the invoker servlet could be used to see the source code of servlets that were generated from JSP pages. Although this may not matter in most cases, it might reveal proprietary code to outsiders, so, as of Tomcat 4.1.12, the invoker was disabled by default in the global deployment descriptor: $CATALINA_HOME/conf/web.xml. I suspect that the Jakarta project will fix the problem soon and re-enable the invoker servlet in upcoming Tomcat releases. In the meantime, however, you almost certainly want to enable it when learning or while developing your application. Just be sure that you do so only on a desktop development machine that is not accessible to the outside world.

To enable the invoker servlet, uncomment the following servlet-mapping element:

<servlet-mapping>
    <servlet-name>invoker</servlet-name>
    <url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
Uncomment the above element in $CATALINA_HOME/conf/web.xml to enable this feature for all Web Applications or in $CATALINA_HOME/webapps/webdba/web.xml for only a particular Web Applications.


Create a Web Application ServletContext

After you have created the Web Application directory structure, (and decided if you are going to turn on the Invoker Servlet feature), you must add a new ServletContext. The ServletContext defines a set of methods and components that a Web Application uses to communicate with the servlet container. The ServletContext acts as a container for the Web Application, and there is only one ServletContext per Web Application.

To build a new ServletContext to Tomcat, you need to add the following entry to the $CATALINA_HOME/conf/server.xml file, setting the values for the path and docBase equal to the name of your new Web Application. This entry should be added inside the element, with the name localhost (or the name you used as the ServerName in Apache's httpd.conf file).

...

<Context path="/webdba"
         docBase="webdba"
         debug="0"
         reloadable="true"
         crossContext="true">
  <Logger className="org.apache.catalina.logger.FileLogger"
          prefix="webdba_log." suffix=".txt"
          timestamp="true"/>
</Context>

...

In the above definition, note that path="/webdba" tells the servlet container that all requests with /webdba appended to the server's URL belong to the webdba Web Application. The second element, docBase="webdba", tells the servlet container that the Web Application exists in the Web Application directory webdba.


Check the Apache JK configuration File (only if connecting with Apache)


Testing the New Web Application Setup


Passing Parameters to Servlets

If you are passing parameters to your servlet through the web.xml file and the getInitParameter() method, you will need to keep the following in mind:

  1. Try to use the init() method to capture your parameters while using the getInitParameter() method.

  2. The getInitParameter() method takes the name of the parameter as a String and returns the value as a String.

  3. If you need to return the parameter value as an Integer, use the following:
    int count;
    public void init() throws ServletException {
      String initial = getInitParameter("Parameter Name");
      try {
        count = Integer.parseInt(initial);
      } catch (NumberFormatException e) {
        count = 0;
      }
    }

  4. After making changes to the web.xml file, you will need to restart Tomcat and Apache.

  5. IMPORTANT: You will need to refer to the servlet using the registered name. If not, the parameter will not be passed and the getInitParameter() method will return null.


Understanding URLs with Web Applications

In the previous example, we created an example servlet called "HelloServlet", registered it as "Hi" and mapped it to the root of the web application. In this case, we can access the "Hi" application using either of the URLs:
http://<machine_name>/WebDBA/Hi
- OR -
http://<machine_name>/WebDBA/servlet/Hi
NOTE: Parameter passing will ONLY work using the registered name. In the example above, both URLs will work since they use the Hi registered name. The following URL though will not work:
http://<machine_name>/WebDBA/servlet/HelloServlet


Setting the CLASSPATH Variable for webapps (JAR Files)

The CLASSPATH variable set in your environment variable is being used only for compiling and running non-servlet applications. The Tomcat server uses a separate runtime environment that DOES NOT inherent your CLASSPATH variable setting.

Classes that are used in one particular web application should be placed in that web application's WEB-INF/classes or in a jar in the WEB-INF/lib as defined by the Servlet 2.2 specification. If you want to give the classes wider scope by putting them in the Common Classloader or Apps Classloader, or want them to be part of the Server Classloader, use one of the following two methods to have those classes included in that classloader:

  1. If the classes are in a jar file, place the jar file in the directory that corresponds to the chosen classloader. If the classes exist as class files, create a classes under the corresponding directory if it doesn't already exist. Then add the class files into the appropriate package directory under the classes directory.

  2. If the chosen classloader is the Common Classloader or Apps classloader you include a directory or jar these classloaders by listing them a System property. For the Common Classloader, include the directory or jar file in a System property named org.apache.tomcat.common.classpath. For the Apps classloader, use a System property named org.apache.tomcat.apps.classpath.

You also have the option of setting the CLASSPATH variable for the Tomcat runtime environment in the $TOMCAT_HOME/bin/tomcat.sh script. There is a section called:

## -------------------- Prepare CLASSPATH --------------------



Last modified on: Saturday, 18-Sep-2010 18:14:00 EDT
Page Count: 2076