ServletConfig

A Configuration object provided by servlet container during its initialization. It contains the initialization parameters which are available in key value pair. Initialized parameter specified in web.xml.

Methods of ServletConfig interface

public String getInitParameter(String name): Returns value for the specified parameter name.

public Enumeration getInitParameterNames(): Returns an enumeration of all the initialization parameter names.

public String getServletName(): Returns servlet name.

public ServletContext getServletContext(): Returns ServletContext Object.

Get ServletConfig Object:

ServletConfig config=getServletConfig(); 

Get init parameter values:

web.xml:

<web-app>  
 <servlet>  
    <servlet-name>DBServlet</servlet-name>  
    <servlet-class>DBServlet</servlet-class>  
  <init-param>  
    <param-name>driverclass</param-name>  
    <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
  </init-param>  
 </servlet>  
 <servlet-mapping>  
    <servlet-name>DBServlet</servlet-name>  
    <url-pattern>/dbexample</url-pattern>  
 </servlet-mapping>  
</web-app>  

In Servlet:

ServletConfig config = getServletConfig();  
String drivername = config.getInitParameter("driverclass");