8

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

0

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.