×
☰ See All Chapters

ServletConfig Interface

ServletConfig is used for getting initialization values or configuration values stored in web.xml. If we store any configuration or initialization values in ServletConfig object then it is available to only that particular servlet instance. Each servlet instance will be having own individual config object. Config object scope is same as servlet instance scope i.e. as long as servlet instance is running inside the container that servlet’s ServletConfig will be there inside the container.

Syntax to specify config parameters inside web.xml

      <servlet>

                <servlet-name>Signup</servlet-name>

                <servlet-class>com.manum.hassan.SignupServlet</servlet-class>

                <init-param>

                        <param-name>driver</param-name>

                        <param-value>com.mysql.cj.jdbc.Driver</param-value>

                </init-param>

                <init-param>

                        <param-name>dname</param-name>

                        <param-value>jdbc:mysql://localhost:3306/study</param-value>

                </init-param>

                <init-param>

                        <param-name>username</param-name>

                        <param-value>root</param-value>

                </init-param>

                <init-param>

                        <param-name>password</param-name>

                        <param-value>root</param-value>

                </init-param>

        </servlet>

Syntax to get the object of ServletConfig

The object of ServletConfig will be created by container and we can get this object by calling getServletConfig() method of Servlet interface.

public class SigninServlet extends HttpServlet {

        public void doPost(HttpServletRequest req, HttpServletResponse res)

                        throws ServletException, IOException {

               

                ServletConfig config=getServletConfig();       

                String driver=config.getInitParameter("driver");  

                String driverName=config.getInitParameter("dname");

                String userName=config.getInitParameter("username");  

                String pwd=config.getInitParameter("password");

 

        }

}

Also we can directly use the ServletConfig config object inside init() method, where ServletConfig object will be pushed to the parameter of init() method.

public class LoginServlet extends HttpServlet {

        public void init(ServletConfig config) {

                String strDriver = config.getInitParameter("driver");

        }

 

        public void doGet(HttpServletRequest req, HttpServletResponse res) {

        }

}

Methods of ServletConfig

String getInitParameter(String name)

Gets the value of the initialization parameter with the given name.

Enumeration<String> getInitParameterNames()

Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.

ServletContext getServletContext()

Returns a reference to the ServletContext  in which the caller is executing.

String getServletName()

Returns the name of this servlet instance.

 


All Chapters
Author