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

Bobby Pardridge
  • 267
  • 6
  • 16

1 Answers1

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.

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50