Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 448
Bobby Pardridge Avatar asked Nov 07 '12 02:11

Bobby Pardridge


People also ask

What are initialization parameters in servlets?

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.

What is init-param in web xml?

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.

What are the initialization parameters explain how do you read initialization parameters in servlets?

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 .

What is the difference between context init parameter and servlet init parameter?

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.


1 Answers

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.

like image 151
Ramesh PVK Avatar answered Oct 05 '22 21:10

Ramesh PVK