Getting and Setting Initialization Parameters for a Servlet - Java Servlet JSP

Java examples for Servlet JSP:Servlet

Description

Getting and Setting Initialization Parameters for a Servlet


public void init() throws ServletException {
    getServletContext().log("getinit init");
    String value = getServletConfig().getInitParameter("param1");

    java.util.Enumeration enum = getServletConfig().getInitParameterNames();
    for (; enum.hasMoreElements(); ) {
        String name = (String)enum.nextElement();
        value = getServletConfig().getInitParameter(name);
    }

    value = getServletContext().getInitParameter("param1");
}
<web-app>
    <servlet>
        <servlet-name>MyServletName</servlet-name>
        <servlet-class>com.mycompany.MyServlet</servlet-class>

        <init-param>
            <param-name> param1 </param-name>
            <param-value> value1 </param-value>
        </init-param>
        <init-param>
            <param-name> param2 </param-name>
            <param-value> value2 </param-value>
        </init-param>
        ...
    </servlet>
    ...
</web-app>

Related Tutorials