You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by chris hutchings <ch...@yahoo.co.uk> on 2001/08/20 15:00:09 UTC

New to servlets: Parameter initialisation in web.xml

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.

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

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 

____________________________________________________________
Do You Yahoo!?
Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
or your free @yahoo.ie address at http://mail.yahoo.ie

Re: New to servlets: Parameter initialisation in web.xml

Posted by "Craig R. McClanahan" <cr...@apache.org>.

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>
    <servlet-name>My Servlet</servlet-name>
    <servlet-class>com.mycompany.mypackage.MyServlet</servlet-class>
    <init-param>
        <param-name>foo</param-name>
        <param-value>bar</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>My Servlet</servlet-name>
    <url-pattern>/mypath</url-pattern>
  </servlet-mapping>

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:

  http://localhost:8080/mypath

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:

  http://localhost:8080/examples/mypath

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

  http://java.sun.com/products/servlet/download.html

> 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


Re: New to servlets: Parameter initialisation in web.xml

Posted by Dmitri Colebatch <di...@bigpond.net.au>.
When you add a context entry in server.xml, you create your own little
context.  the path that you name in docBase should contain a WEB-INF
directory, which contains its own web.xml file.  that is the web.xml file
that will be read for requests to that context.

if you're after a simple way to get started, I'd suggest editing the
HelloServlet from the examples context, and editing that then
recompiling... change the webapps/examples/WEB-INF/web.xml file for your
context params.

hth, cheers
dim

On Mon, 20 Aug 2001, [iso-8859-1] 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.
> 
> i have used the web.xml in ROOT\WEB-INF 
> 
> 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 
> 
> ____________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
> or your free @yahoo.ie address at http://mail.yahoo.ie
>