tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: New to servlets: Parameter initialisation in web.xml
Date Mon, 20 Aug 2001 15:19:42 GMT

On Mon, 20 Aug 2001, chris hutchings wrote:

> Hi,
> i am new to servlets, and i am working through the
> examples in a servlets book,
> I have to set some <init-param> parameters in web.xml
> that are used in one of the examples init() method.
> Unfortunately the initialisation is not working
> properly and i get a 'null' when I print out the
> variable.
> what can i be doing wrong.

In order for servlet initialization parameters to be accessed by your
servlet, you have to do *four* things:

* Set up the <init-param> entries inside your <servlet> definition.

* Make sure your servlet class is under /WEB-INF/classes (or in a
  JAR file under /WEB-INF/lib) in a directory structure that reflects
  the package names (just like any other Java class).

* Set up a <servlet-mapping> that maps some particular request URI
  to your servlet.

* Access the server using that request URI.

> i have used the web.xml in ROOT\WEB-INF 

Let's carry this out with an example.  Assume you add the following to the
ROOT/WEB-INF/web.xml file in the appropriate places):

    <servlet-name>My Servlet</servlet-name>

    <servlet-name>My Servlet</servlet-name>

Now, assuming that you have your servlet class in the right place
(ROOT/WEB-INF/classes/com/mycompany/mypackage/MyServlet.class), you'll be
able to restart Tomcat and access your servlet like this:


and it will be able to read the initialization parameter "foo".

The URL above works because you were modifying the ROOT webapp.  If you
had modified the "examples" webapp instead, the only difference would have
been in the URL:


For more information about valid parameters in web.xml, you should
download and read the Servlet Specification at:

> Also I have added a context path to server.xml, so
> that I can use my own named directories.
> But this does not seem to work either.
> I can run servlets in the
> \webapps\ROOT\WEB-INF\classes folder, but I can not
> get the servlets in the examples directory to work. 
> It must be something basic, with the configuration but
> what I do not know ……
> Chris 

Craig McClanahan

View raw message