Followers

Servlet: ServletContext

Usage Of ServletContext Object in Servlet Application.

1. Server will create an object of ServletContext interface for every application which is running at the server.
2. This ServletContext object is created whenever we start the server and this object is automatically destroyed when shut down the server.
3. We can use this ServletContext object in the servlet application to store the data of the client temporarily at the server.
4. We use the following method of HttpServlet class to get the reference of this ServletContext object
public ServletContext getServletContext();
5. Every value must be stored into ServletContext object along with some attributes.
6. We can use the following method of ServletContext interface to store the data into the ServletContext object.i.e.
public void setAttribute(String attr.name, Object attr.value);
Where Object is the class available in java.lang package , this is the root class or parent class for all the object in java application.
7. We can use the following method of ServletContext interface to get the data from ServletContext object i.e.
public Object getAttribute(String attr.name);
8. This ServletContext will act as the global object for all the servlet classes of the respective servlet application which means we can access this ServletContext object from all the servlet classes of the application.
9. This ServletContext object is automatically destroyed by the server whenever we shut down the server. So all the data of ServletContext object is automatically destroyed.
10. The following example demonstrate how to use this ServletContext object in servlet application to store the data of the client temporarily at the server.


index.html
=======

<html>
    <body>
        <form action="first" method="get">
            <p>Please enter your name : <input type="text" name="sname" ></p>
            <p>Please enter your address : <input type="text" name="saddress" ></p>
            <p>Please enter your Age : <input type="text" name="sage" ></p>
            <p><input type="submit" value="Send" ></p>
           
        </form>
    </body>
</html>



web.xml
======

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>first</servlet-name>
        <servlet-class>Context1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>first</servlet-name>
        <url-pattern>/first</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>second</servlet-name>
        <servlet-class>Context2</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>second</servlet-name>
        <url-pattern>/second</url-pattern>
    </servlet-mapping>
   
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

The URL pattern of this class must be /first this will receive the form request with 4 parameters .i.e. sname, saddress sage and Send as get request.

Context1.java
=========

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Context1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("sname");
        String address = request.getParameter("saddress");
        String age = request.getParameter("sage"); 
        ServletContext ctxt = getServletContext();
                   /*
                   Where getServletContext() is the method of super class i.e. HttpServlet class that will return the reference of ServletContext interface which is already created by server.      
                   */
        ctxt.setAttribute("sname", name);
                   /*
                   Where setAttribute() is the method of ServletContext interface that is stored the name value of the client along with the attr.name is sname into ServletContext object.
                   */
        ctxt.setAttribute("saddress", address);
        ctxt.setAttribute("sage", age);
        out.println("<A href=/servletContextEx/second> Show my details </A>");       
   }
}

The URL pattern of this class must be /second this will receive the form request without any parameters when the client click the anchoring message.

Context2.java
=========

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Context2 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        ServletContext ctxt = getServletContext();
                   /*
                   This will return the reference of same ServletContext object where there are 3 attributes and values.
                   */
        String name = (String)ctxt.getAttribute("sname");
                   /*
                   Where getAttribute() is the method of ServletContext interface that will return the value of sname attribute from ServletContext object as Object class reference so we need to type cast this returned reference into String class.
                   */
        String address = (String)ctxt.getAttribute("saddress");
        String age = (String)ctxt.getAttribute("sage");
        out.println("<br/><br/>");
        out.println("Your name is  " + name);
        out.println("<br/><br/>");
        out.println("Your address is "+address);           
        out.println("<br/><br/>");
        out.println("Your Age is  "+age);           
        out.println("<br/><br/>");
    }
}

No comments:

Post a Comment