How to retrieve <servlet><init-param> value from web.xml in Servlet?

Anand picture Anand · Dec 30, 2011 · Viewed 12.6k times · Source

I need to retrieve init-param value from xml to Servlet i used following code

<servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>LoginServlet</servlet-class>
    <init-param>
        <param-name>jdbcDriver</param-name>
        <param-value>com.mysql.jdbc.Driver</param-value>
    </init-param>
</servlet>

servlet code

public void init(ServletConfig config) throws ServletException {
    super.init(config);
    System.out.println(config.getInitParameter("jdbcDriver"));
}

But It displayed null .. could any one help me to do that . thanks in advance

Answer

nIcE cOw picture nIcE cOw · Dec 30, 2011

I can't see a single reason, why you have to override your init(ServletConfig sc) method, since you can always get your ServletConfig by calling your inherited getServletConfig() method.

System.out.println(getServletConfig().getInitParameter("jdbcDriver"));