Servlet Tutorial

ServletConfig

ServletConfig (one per SERVLET):

  • ServletConfig is an interface which is present in javax.servlet.* package.
  • The purpose of ServletConfig is to pass some initial parameter values, technical information (driver name, database name, data source name, etc.) to a servlet.
  • An object of ServletConfig will be created one per servlet.
  • An object of ServletConfig will be created by the server at the time of executing public void init (ServletConfig) method.
  • An object of ServletConfig cannot be accessed in the default constructor of a Servlet class. Since, at the time of executing default constructor ServletConfig object does not exist.
  • By default ServletConfig object can be accessed with in init () method only but not in doGet and doPost. In order to use, in the entire servlet preserve the reference of ServletConfig into another variable and declare this variable into a Servlet class as a data member of ServletConfig.
  • For example:

    class x extends HttpServlet

  • When we want to give some global data to a servlet we must obtain an object of ServletConfig.
  • web.xml entries for ServletConfig
<servlet>
    .........
    <init-param>
        <param-name>Name of the parameter</param-name>
        <param-value>Value of the parameter</param-value>
    </init-param>
    ........
</servlet>

For example:

<servlet>
    <servlet-name>abc</servlet-name>
    <servlet-class>serv1</servlet-class>
    <init-param>
        <param-name>v1</param-name>
        <param-value>10</param-value>
    </init-param>
    <init-param>
        <param-name>v2</param-name>
        <param-value>20</param-value>
    </init-param>
</servlet>

The data which is available in ServletConfig object is in the form of (key, vlaue)