ServletConfig Interface


  • A web container is responsible for creating ServletConfig object for each servlet.
  • The ServletConfig object passes the information to a Servlet during initialization.
  • The advantage of ServletConfig is that it eliminates the need to edit the servlet file if the information is modified from the web.xml file.

ServletConfig Interface Methods

getInitParameter(java.lang.String name)Returns a String containing the value of the name initialization parameter
getInitParameterNames( )Returns the name of the servlet initialization parameters.
getServletContext( )Returns the reference of the ServletContext.
getServletName( )Returns the name of the servlet instance.

Example : Illustrating the use of ServletConfig Interface




import javax.servlet.*;
import javax.servlet.http.*;
public class ServletDemo extends HttpServlet
      protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
           PrintWriter pw = response.getWriter();
           ServletConfig sc = getServletConfig();