How are initialization parameters passed to a servlet under Servlet API version 2.2?

Avi Kak

Through the webapp deployment descriptor, which is the web.xml file in the directory WEB-INF.

Note that there are two different types of web.xml files in Tomcat 3.x: There is a web.xml that sits in the top-level configuration directory TOMCAT_HOME/conf and then there can be a separate web.xml for each webapp in the directory WEB-INF for that webapp. You would want to keep webapp specific deployment information, such as initialization parameters and their values, in the web.xml specific to that webapp.

The best on-line example that illustrates servlet initialization through a webapp's deployment descriptor is, I believe, the ServletParam servlet in the test webapp in your Tomcat software package. This servlet can be called with two different canonical names, as for example in

   http://localhost:8080/test/servlet/servletParam1
and
   http://localhost:8080/test/servlet/servletParam2
The deployment descriptor maps the canonical names servletParam1 and servletParam2 to the actual servlet ServletParam but with different values of the initialization parameters. When the servlet ServletParam is called with the name servletParam1, the initialization parameter-value pairs are
    param1 value1
    param2 value2
and when the same servlet is called with the name servletParam2, the parameter-value pairs are
    param3 value3
    param4 value4
The following constitutes a sufficient deployment descriptor for this example:
<!--  filename: web.xml in the directory TOMCAT_HOME/webapps/<your-webapp>/WEB-INF/ -->

<?xml version="1.0"?>

<!DOCTYPE web-app SYSTEM "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"> 

<web-app>
    <servlet>
        <servlet-name>
            servletParam1
        </servlet-name>
        <servlet-class>
            ServletParam
        </servlet-class>
        <init-param>
            <param-name>param1</param-name>
            <param-value>value1</param-value>
        </init-param>
        <init-param>
            <param-name>param2</param-name>
            <param-value>value2</param-value>
        </init-param>
    </servlet>          

    <servlet>
        <servlet-name>
            servletParam2
        </servlet-name>
        <servlet-class>
            ServletParam
        </servlet-class>
        <init-param>
            <param-name>param3</param-name>
            <param-value>value3</param-value>
        </init-param>
        <init-param>
            <param-name>param4</param-name>
            <param-value>value5000</param-value>
        </init-param>
    </servlet>          
</web-app>

The first half of the web.xml file associates the servlet ServletParam with the name servletParam1 and then goes on to declare its parameter-value pairs. The second half does the same for the name servletParam2.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact