Servlet Tutorial

Obtain an Object of Servlet Context

Obtain an Object of Servlet Context:

In order to get an object of ServletContext we have two ways, they are by calling getServletContext () method directly and by making use of ServletConfig interface.

By using getServletContext () method:

getServletContext () method is defined in GenericServlet and it is inherited into HttpServlet and it is further inherited into our own servlet class. Hence, we can call getServletContext () method directly.

For example:

public class Serv1 extends HttpServlet
{
    public doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
        ...........
        ...........
        ServletContext ctx=this.getServletContext ();
        ............
        ............
    }
};

By using ServletConfig interface:

In ServletConfig interface we have the following method which gives an object of ServletContext.

In order to call the above method first we must obtain an object of ServletConfig interface and later with that object we can call getServletContext () method.

For example:

public class Serv2 extends HttpServlet
{
    public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
        .......
        .......
        ServletConfig config=this.getServletConfig ();
        ServletContext ctx=config.getServletContext ();
        ........
        ........
    }
};

Number of ways to RETRIEVE THE DATA from an OBJECT of ServletContext:

In ServletContext interface we have the following methods to retrieve the value of context parameter by passing context parameter name.

getInitparameter

For example:

ServletContext ctx=getServletContext (); 
String val1=ctx.getInitParameter ("v1");
String val2=ctx.getInitParameter ("v2");
getInitparametername

For example:

ServletContext ctx=getServletContext ();
Enumeration en=ctx.getInitParameterNames ();
While (en.hasMoreElements ())
{
    String cpn= (String) en.nextElement ();
    String cpv=ctx.getInitParameter (cpn); 
    pw.println (cpv+" is the value of "+cpn);
}
getdataservletconfig

Differences between ServletConfig and ServletContext interfaces:

ServletConfigServletContext
An object of ServletConfig exists one per servlet.An object of ServletContext exists one per web application.
An object of ServletConfig will be created when init (ServletConfig) method is executed.An object of ServletContext will be created when we deploy the web application in servlet container or servlet execution environment.
ServletConfig object contains a specific data to a particular servlet.ServletContext object contains a common or global data to 'n' number of servlets and 'n' number of JSP's.
The data to a servlet which related to ServletConfig object must be written in <init-param>...</init-param> with in <servlet>...</servlet> of web.xmlThe common data or global data related to ServletContext must be written under <context-param>...</context-param> with in <web-app>...</web-app> and outside of <servlet>...</servlet> of web.xml
An object of ServletConfig will exists as long as a specific servlet is executing.An object of ServletContext will exists until the entire web application completed its execution.

Write a servlet which illustrate the concept of ServletContext?

Answer:

web.xml:

<web-app>
    <context-param>
        <param-name>v1</param-name>
        <param-value>10</param-value>
    </context-param>
    <context-param>
        <param-name>v2</param-name>
        <param-value>20</param-value>
    </context-param>
    <servlet>
        <servlet-name>abc</servlet-name>
        <servlet-class>Serv1</servlet-class>
        <init-param>
            <param-name>v3</param-name>
            <param-value>30</param-value>
        </init-param>
    </servlet>
    <servlet>
        <servlet-name>pqr</servlet-name>
        <servlet-class>Serv2</servlet-class>
        <init-param>
            <param-name>v4</param-name>
            <param-value>40</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>abc</servlet-name>
        <url-pattern>/firstserv</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>pqr</servlet-name>
        <url-pattern>/secondserv</url-pattern>
    </servlet-mapping>
</web-app>

Serv1.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Serv1 extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        ServletConfig config = getServletConfig();
        ServletContext ctx = config.getServletContext();
        String val1 = ctx.getInitParameter("v1");
        String val2 = ctx.getInitParameter("v2");
        String val3 = config.getInitParameter("v3");
        String val4 = config.getInitParameter("v4");
        int sum = Integer.parseInt(val1) + Integer.parseInt(val2);
        pw.println("<h3> Value of v1 is " + val1 + "</h3>");
        pw.println("<h3> Value of v2 is " + val2 + "</h3>");
        pw.println("<h3> Value of v3 is " + val3 + "</h3>");
        pw.println("<h3> Value of v4 is " + val4 + "</h3>");
        pw.println("<h2> Sum = " + sum + "</h2>");
    }
};

Serv2.java:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Serv2 extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html");
        PrintWriter pw = res.getWriter();
        ServletContext ctx = getServletContext();
        Enumeration en = ctx.getInitParameterNames();
        while (en.hasMoreElements()) {
            Object obj = en.nextElement();
            String cpname = (String) obj;
            String cpvalue = ctx.getInitParameter(cpname);
            pw.println("</h2>" + cpvalue + " is the value of " + cpname + "</h2>");
        }
    }
};