Common init-parameters in web.xml for multiple java servlets?
Asked Answered
D

1

7

My current understanding is that init-params in the web.xml must be put in the body of a servlet variable, like this:

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>MyServlet</servlet-class>

    <init-param>
        <description>debug</description> 
        <param-name>debug</param-name> 
        <param-value>true</param-value> 
    </init-param>   
</servlet>

Which works fine, but if I bring the init-param outside the servlet body, then it no longer recognizes it when I call getInitParam()

Just wondering if it was possible, since I have 3 servlets that I would like to share common init parameters

Ditchwater answered 7/11, 2012 at 2:59 Comment(0)
F
11

No, you cannot achieve that using servlet init-param. If you want common init-param across servlets you should use Context Parameters.

This is how you can do that:

<context-param>
    <description>debug</description> 
    <param-name>debug</param-name> 
    <param-value>true</param-value>
</context-param>

And, use ServletContext.getInitParameter() within the servlet.

Faris answered 7/11, 2012 at 5:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.