ServletConfig Interface

In the Java Servlet API, the ServletConfig objects represent the configuration of a servlet. The configuration information contains initilization parameters (a set of name/value pairs), the name of the servlet, and a ServletContex object, which gives the servlet infotmation about the conatiner.
The initilization parameters and the name of a servlet can be specified in the deployment descriptor (web.xml file).
public interface ServletConfig
The ServletCofig interface encapsulates the notion of a config for a specific servlet. An object of thes type can be obtained using the getServletConfig() method of the Servlet interface.
Method of ServletConfig Interface
  1.  public String getServletName()
               This method returns the name assigned to a servlet in its deployment descriptor. If no name is specified, this returns the servlet name instead.
  2. public ServletContext getServletContext()
               This method returns a reference to the ServletContext object associated with the web application.
  3. public String getInitParameter(String name)
               This method returns the value of a named initialization parameter, or null if the specified parameter does not exist.
  4. public Enumeration getInitParameterNames()
               This method returns an enumeration of all the initialization prameters of a servlet.
Obtaining a reference to ServletConfig
public ServletConfig getServletConfig()
           This method returns an object of ServletConfig. Servlet can also accesss the ServletConfig object by calling the getServletConfig() method. This method is specified int the javax.servlet.Servlet interface.
Example of ServletConfig
import; import; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException { response.setContentType("text/html"); PrintWriter out=response.getWriter(); ServletConfig config=getServletConfig(); String name=config.getInitParameter("adminName"); String password=config.getInitParameter("password"); out.println("Welcome Servlet initilization Parameters"); out.println("<BR>"); out.println("User Name is "+name); out.println("<BR>"); out.println("Password is "+password); out.println("<BR>"); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5"> <display-name>ServletConfigDemo</display-name> <servlet> <description></description> <display-name>FirstServlet</display-name> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> <init-param> <param-name>adminName</param-name> <param-value>Sunil</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>Kumar</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> </web-app>
ServletConfig interface
Download this example
1) Deployed using Tomcat 6.0
2) Deployed using eclipse IDE


Latest Tutorial

Most Visited Tutorial