tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@eng.sun.com>
Subject Re: Frustration with getInitParameter
Date Sat, 28 Oct 2000 00:22:46 GMT
Paul Hoepfner-Homme wrote:

> Sorry, typo in web.xml below.  I had
> <servlet-param>MyServlet</servlet-param> before, but it should be
> <servlet-class> obviously.  I have fixed it below.
>
>> Finally, I'm not alone!!  I have 3.2 beta 6 as well.  Here is a how
>> I have web.xml set up:
>>
>> <web-app>
>>     <context-param>
>>         <param-name>test1</param-name>
>>         <param-value>here1</param-value>
>>     </context-param>
>>     <servlet>
>>         <servlet-name>myservlet</servlet-name>
>>         <servlet-class>MyServlet</servlet-class>
>>         <init-param>
>>             <param-name>test2</param-name>
>>             <param-value>here2</param-value>
>>         </init-param>
>>     </servlet>
>> </web-app>
>>
>> I can access the "test1" parameter by calling
>> getServletConfig().getServletContext().getInitParameter("test1").
>> But I cannot access the "test2" parameter at all.  For example
>> calling getServletConfig().getInitParameter("test2") returns null.
>>
>> Paul
>>
>
What request URI are you trying to use to access this servlet?  If you
are trying something like:

    http://localhost:8080/myapp/servlet/myservlet

or

    http://localhost:8080/myapp/servlet/MyServlet

(where "/myapp" is the context path of your application), it is not
going to work.  The reason is that "/servlet/myservlet" runs the invoker
servlet, which indirectly loads and executes yours.  To access servlet
initialization parameters, you will also need to add a servlet mapping
for this servlet, and then call it.  Add this to the bottom of your
web.xml (before the </web-app> entry)

    <servlet-mapping>
        <servlet-name>myservlet</servlet-name>
        <url-pattern>/foo/*</url-pattern>
    </servlet-mapping>

and then execute:

    http://localhost:8080/myapp/foo

and see what happens.

NOTE:  It is quite possible that some servlet containers will return
initialization parameters for a servlet accessed via something like
"/servlet" anyway.  That's legal, because the whole idea of an "invoker"
servlet is not in the servlet spec, and is therefore not guaranteed to
be portable.  Using a servlet mapping and executing your servlet that
way, on the other hand, *is* in the spec and will work on any 2.2 or 2.3
compliant servlet container.

Craig McClanahan



Mime
View raw message