×
☰ See All Chapters

ServletContext Interface

ServletContext is used for getting initialization values or configuration values stored in web.xml. If we store any configuration or initialization values in ServletContext object then it is available to the entire servlet instance. All servlet instances will be having one ServletContext object. ServletContext object can be accessed by all the servlets and by all the users in the application. Only one ServletContext instance will be created per application.

Syntax to specify context parameters inside web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app >

        <context-param>

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

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

        </context-param>

        <context-param>

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

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

        </context-param>

 

        <context-param>

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

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

        </context-param>

 

        <context-param>

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

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

        </context-param>

       

        <servlet>

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

                <servlet-class>com.java4coding.SignupServlet</servlet-class>

        </servlet>

 

        <servlet-mapping>

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

                <url-pattern>/signup</url-pattern>

        </servlet-mapping>

 

        <servlet>

                <servlet-name>Signin</servlet-name>

                <servlet-class>com.java4coding.SigninServlet</servlet-class>

        </servlet>

 

        <servlet-mapping>

                <servlet-name>Signin</servlet-name>

                <url-pattern>/signin</url-pattern>

        </servlet-mapping>

</web-app>

Syntax to get the object of ServletConfig

The object of ServletConfig will be created by calling getServletContext() method of ServletConfig interface or getServletContext() method of GenericServlet class

public class SigninServlet extends HttpServlet {

        public void doPost(HttpServletRequest req, HttpServletResponse res)

                        throws ServletException, IOException {

 

                ServletContext context = getServletContext();

                // Or we can get ServletContext from ServletConfig object

                // ServletContext context=getServletConfig().getServletContext();

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

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

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

                String pwd = context.getInitParameter("password");        }

}

Methods of ServletContext used for storing or retrieving values

String getInitParameter(String name)

Returns a String containing the value of the named context-wide initialization parameter, or null if the parameter does not exist.

Enumeration getInitParameterNames()

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

void setAttribute(String name,Object object)

Binds an object to a given attribute name in this ServletContext.

Object getAttribute(String name)

Returns the servlet container attribute with the given name, or null if there is no attribute by that name.

void removeAttribute(String name)

Removes the attribute with the given name from this ServletContext.

Using setAttribute() we can set the values in one servlet and this can be used by any servlet instance with in the same container.

public class SigninServlet extends HttpServlet {

        public void doPost(HttpServletRequest req, HttpServletResponse res)

                        throws ServletException, IOException {

                boolean b = false;

               

                ServletContext context = getServletContext();

                //TO set the attribute value

                context.setAttribute("GlobalMessage", "Hello Servlet");       

        }

}

 

public class SignUpServlet extends HttpServlet {

        public void doPost(HttpServletRequest req, HttpServletResponse res)

                        throws ServletException, IOException {

                boolean b = false;

               

                ServletContext context = getServletContext();

                //To retrieve the attribute value

                String message = (String) context.getAttribute("GlobalMessage");

                //To remove attribute value

                context.removeAttribute("GlobalMessage");       

       

        }

}

 


All Chapters
Author