JSP – PageContext Implicit Object

JSP PageContext Implicit Object

  • JSP pageContext object is an instance of javax.servlet.jsp.PageContext.
  • This object is used to manipulate page, request, application and session attributes.

login.jsp:

<html>
            <head>
                        <title>login</title>
            </head>
            <body>
                        <form action=”welcome.jsp”>
                                    <input type=”text” name=”userName” />
                                    <input type=”submit” value=”login”/>
                        </form>
            </body>
</html>

welcome.jsp:

<html>
            <head>
                        <title>pageContext implicit object example</title>
            </head>
            <body>
                        <%
                                    String userName = request.getParameter(“userName”);
                                    if(userName.equals(“amar”))
                                    {
                                                pageContext.setAttribute(“userName”, userName, PageContext.SESSION_SCOPE);
                                                response.sendRedirect(“home.jsp”); 
                                    }                      
                                    else
                                    {
                                                out.print(“Wrong username.”); 
                                    }
                        %>
            </body>
</html>

home.jsp:

<html>
            <head>
                        <title>home</title>
            </head>
            <body>
                        <h3>This is user home page.</h3>
                        <%
                                    String userName = (String)pageContext.getAttribute(“userName”,
                                                            PageContext.SESSION_SCOPE);
                                    out.print(“Logged in user: ” + userName);
                        %>
            </body>
</html>
Scroll to Top