JSP Impilicit Objects - pageContext


JSP pageContext -

pageContext is an object of javax.servlet.jsp.JspContext class that enables us to set and get attributes from different scopes supported by JSP.
In addition to servlet scopes (request, session, application), JSP has an extra scope - page scope
By default attributes in PageContext has page scope, to set a different scope PageContext class defines the following fields -

1- Request Scope - Attributes set with request scope will remain alive as long as the request. PageContext defines the following constant field to set request scope -
public static final int REQUEST_SCOPE

2- Session Scope - Attributes set with session scope will remain alive as long as the session exists. PageContext defines the following constant field to set session scope -
public static final int SESSION_SCOPE

3- Application Scope - Attributes set with application scope will remain alive as long as the web-app. They destroyed when application goes down. PageContext defines the following constant field to set application scope -
public static final int APPLICATION_SCOPE

4- Page Scope - Attributes set with page scope will remain alive for the current page only. PageContext defines the following context field to set page scope -
public static final int PAGE_SCOPE


To set and get attributes, PageContext inherits methods from JspContext.

Fig:- PageContext inheriting JspContex



Using pageContext to set and get attributes -


1- setting a request level attribute -

<%
String str = new String("Hello");
pageContext.setAttribute("msg",str, PageContext.REQUEST_SCOPE);
%>
getting it -
<%
Object obj = pageContext.getAttribute("msg", PageContext.REQUEST_SCOPE);
String s = (String)obj;
%>


2- setting a session level attribute -
<%
String str = new String("Hello");
pageContext.setAttribute("msg",str, PageContext.SESSION_SCOPE);
%>
getting it -
<%
Object obj = pageContext.getAttribute("msg", PageContext.SESSION_SCOPE);
String s = (String)obj;
%>


3- setting a page level attribute -
<%
String str = new String("Hello");
pageContext.setAttribute("msg",str, PageContext.PAGE_SCOPE);
Or
pageContext.setAttribute("msg",str);  
// do not need to specify scope here, because by default pageContext has PAGE_SCOPE.
%>
getting it -
<%
Object obj = pageContext.getAttribute("msg", PageContext.SESSION_SCOPE);
Or
Object obj = pageContext.getAttribute("msg");
String s = (String)obj;
%>
In the same way we can set and get application level attribute.

Example -

In this example we will use pageContext to set user name attribute in session scope and use it on another page.

1- index.html or index.jsp - This will be our default page.
<html>
    <head>
        <title>PageContext Demo</title>
    </head>
    <body>
        <form method="post" action="LoginCheck.jsp">
            UserName - <input type="text" name="t1"><br>
            Password - <input type="password" name="t2"><br>
            <input type="submit" value="Login">
            <input type="reset" value="Reset">
        </form>
    </body>
</html> 


2- LoginCheck.jsp - Create a jsp file named LoginCheck.jsp. Here we will check username and password and set a session scope PageContext variabel that we will use on the next page(Home.jsp).
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>JSP Page</title>
    </head>
    <body>
        <%
        String nm = request.getParameter("t1");
        String ps = request.getParameter("t2");
        if(nm.equals("Rohit") && ps.equals("123"))
        {
            pageContext.setAttribute("name", nm, PageContext.SESSION_SCOPE);
            response.sendRedirect("Home.jsp");
        }
        %>
    </body>
</html>        


3- Home.jsp - Create a jsp file named Home.jsp, here we will use PageContext variable set in LoginCheck page.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Home Page</title>
    </head>
    <body>
        <% 
        String name = (String)pageContext.getAttribute("name", PageContext.SESSION_SCOPE);
        out.println("<h1>Welcome Home "+ name +"</h1>");
        %>
    </body>
</html>        


4- Now run the project, it will show you login form, enter user name and password(Rohit and 123 in our case).It will redirect you to home page that will show your username.





Download this example (NetBeans implementation) - Click Here