Generic init parameters in web.xml for multiple Java servlets?

My real understanding is that init-params in web.xml should be placed in the body of a servlet variable, for example:

<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 init-param outside the servlet body, it will no longer recognize it when I call getInitParam ()

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

+6
source share
1 answer

No, you cannot achieve this using the init-param of the servlet. If you need a generic init-param for servlets, you should use context options.

Here's how you can do it:

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

And use ServletContext.getInitParameter () in the servlet.

+10
source

All Articles