Deploying Web Applications to Tomcat
Pages: 1, 2, 3, 4, 5

The next step in deploying the login servlet is to add a servlet entry into the web application's web.xml file. An example <servlet> element can be found in the following code snippet.

It isn't necessary to add all servlets to the web.xml file; it's only necessary when the servlet requires additional information, such as initialization parameters.

Example <servlet> Element


Related Apache Articles on O'Reilly Network's

Getting, Installing, and Running Apache

Securing Your Apache Serve (Chapter 3 from Apache: The Definitive Guide)

AxKit: An XML-Delivery Toolkit for Apache

An Amble Through Apache Configuration

Also in Using Tomcat:

Configuring Tomcat with IIS Web Server

Configuring Tomcat and Apache With JK 1.2

Demystifying Tomcat 4's server.xml File

Embedding Tomcat Into Java Applications

Using SOAP with Tomcat

This servlet entry contains a simple servlet definition. A description of each of its parts can be found in Table 2.

Table 2. The Sub-elements of a <servlet>




The <servlet-name> element is simply the canonical name of the deployed servlet.


The <servlet-class> sub-element references the fully qualified class name of the servlet.


The <init-parameter> sub-element is an optional parameter containing a name-value pair that is passed to the servlet on initialization. It contains two sub-elements, <param-name> and <param-value>, which contain the name and value, respectively, to be passed to the servlet.


The <load-on-startup> sub-element indicates the order in which each servlet should be loaded. Lower positive values are loaded first. If the value is negative or unspecified, then the container can load the servlet at anytime during startup.

To add our login servlet we need to make the following entry into the TOMCAT_ROOT/onjava/WEB-INF/web.xml file:


That's all there is to it. To see your web application in action, restart the Tomcat server and open the following URL in your browser:


You should see an image similar to Figure 1 (which was referred to above). Now enter a username and password and press the "Submit Query" button. If everything went according to plan, you should see an image similar to Figure 2.

Screen shot.
Figure 2. Results after pressing "Submit Query".

If you didn't see an image similar to Figure 2, make sure that you have the servlet class in the appropriate directory and the entry in the web.xml file matches the code snippet referenced above.

Adding Tag Libraries

The final component that we're adding is a tag library. This library contains a single tag, HelloTag, that replaces every occurrence of the text <onjava:hello/> with the literal string "Hello". While this is a perfectly silly example of a tag library, it allows us to present a practical example of deploying a tag library. The source code for the tag handler can be found in Listing 5 and the source for the tld can be found in Listing 6.

Listing 5

package com.onjava;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

public class HelloTag extends TagSupport
 public void HelloTag() {


 // Method called when the closing hello tag is encountered
 public int doEndTag() throws JspException {

  try {

   // We use the pageContext to get a Writer
   // We then print the text string Hello
  catch (Exception e) {

   throw new JspTagException(e.getMessage());
  // We want to return SKIP_BODY because this Tag does not support
  // a Tag Body
  return SKIP_BODY;

 public void release() {

  // Call the parent's release to release any resources
  // used by the parent tag.
  // This is just good practice for when you start creating
  // hierarchies of tags.

Listing 6 taglib.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
  PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"

<!-- a tag library descriptor -->


 <info>Just Says Hello</info>

To deploy this tag library, we need to make an entry to the web.xml file. The modified web.xml file can be found in Listing 7.

Listing 7 web.xml

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

 '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'





The added <taglib> entry contains two elements. The first <taglib> element, <taglib-uri>, tells the container how the tag library is to be referenced. For this example we use the value /onjava, which is the way we'll reference the tag library in our JSPs.

The second <taglib> element, <taglib-location>, defines the location of the tag library's descriptor (TLD). The TLD defines the tags contained in the library and the handlers that will process the defined tags.

Pages: 1, 2, 3, 4, 5

Next Pagearrow