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
You can specify initial parameter values for servlets in web modules during or after installation of an application onto a WebSphere® Application Server deployment target. The <param-value> values specified in <init-param> statements in the web. xml file of web modules are used by default.
This is an element within the servlet. The optional init-param element contains a name/value pair as an initialization parameter of the servlet. Use a separate set of init-param tags for each parameter. You can access these parameters with the javax.
To retrieve initialization parameters, call the getInitParameter(String name) method from the parent javax. servlet. GenericServlet class. When passed the name of the parameter, this method returns the parameter's value as a String .
Servlet init parameters are for a single servlet only. Nothing outside that servlet can access that. It is declared inside the <servlet> tag of Deployment Descriptor, on the other hand context init parameter is for the entire web application. Any servlet or JSP in that web application can access context init parameter.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With