ServletContext

ServletContext is an in-built interface present in javax.servlet package. ServletContext object is available one per web application. When the web application is deployed, ServletContext object is automatically created by the web container. This object can be used to get configuration information from web.xml file. Assume there exist a web application with 2 servlet classes, and they need to get some technical values from web.xml, in this case ServletContext concept will works great, i mean all servlets in the current web application can access these context values from the web.xml

servlet_context

ServletContext

There are 3 approaches to get ServletContext object.


Approach-1
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
Approach-2

When we directly call getServletContext() method available in GenericServlet class, then we will get the ServletContext object. In general we are extending our class with HttpServlet, but we know HttpServlet is the sub class of GenericServlet.

Example;
public class Java8s extends HttpServlet
{
    public void doGet/doPost()
    {
        //
    }
    ServletContext context = getServletContext();
}
Approach-3

We can get the object of ServletContext by making use of HttpServletRequest object, we have direct method in HttpServletRequest interface.

Example;
public class Java8s extends HttpServlet
{
    public void doGet/doPost(HttpServletRequest req,-)
    {
        ServletContext ctx = req.getServletContext();
    }
}
Example;
public class Java8s extends HttpServlet
{
    public void doGet/doPost(HttpServletRequest req,-)
    {
        ServletContext ctx = req.getServletContext();
    }
}

let's see a demo program:

index.html

<form action="MyServlet1" method="post">
    <input type="submit" value="go">
    </form>
</font>
MyServlet1.java
package java8s;
 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*
public class MyServlet1 extends HttpServlet
{
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
    {
        response.setContentType("text/html");
 
        PrintWriter pw=response.Writer;
        // I am using 2nd way to create Context object
        ServletContext context=getServletContext();  
 
        String s1=context.getInitParameter("n1");
        String s2=context.getInitParameter("n2");
 
        pw.println("n1 value is " +s1+ " and n2 is " +s2);
 
       pw.close();
    }
}
web.xml
<web-app>
 
    <context-param>
        <param-name> n1 </param-name>
        <param-value> 1000 </param-value>
    </context-param>
 
    <context-param>
        <param-name> n2  </param-name>
        <param-valuev 2000 </param-value>
    /context-param>
 
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>java8s.MyServlet1</servlet-class>
    </servlet>

    <servlet-mapping>
            <servlet-name>abc</servlet-namev
            <url-pattern>/MyServlet1</url-patternv
    </servlet-mappingv
 
   <welcome-file-listv  
        <welcome-file>index.html</welcome-filev  
   </welcome-file-list>
 
</web-app><web-app>

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext