Advertisement



< Prev
Next >



JSP pageContext object





JSP inbuilt pageContext object refers to the context of the current JSP web page. Using it we can get, set and remove attributes present in the various scopes such as page, session, request and application scope. It is an object of type javax.servlet.jsp.PageContext class.




Methods of PageContext class


Methods Description
Object setAttribute(String name, Object value) This method sets an attribute in the page scope
Object setAttribute(String name, Object value, int scope) This method returns the attribute in the specified scope.
Object getAttribute(String name) This method returns the attribute in the page scope or null.
Object getAttribute(String name, int scope) This method returns the attribute in the specified scope or null.
Object findAttribute(String name) This method returns the value associated with the attribute searched in page, request, session and application scope, or null.
void removeAttribute(String name) This method removes an attribute(if found in any scope).
void removeAttribute(String name, int scope) This method removes an attribute from the specified scope(if found).
String forward(String relativeUrlPath) This method forwards the ServletRequest and the ServletResponse to another web page in the application.
ServletRequest getRequest() Returns the current ServletRequest object.
ServletResponse getResponse() Returns the current ServletResponse object.
ServletConfig getServletConfig() Returns the current ServletConfig object.
ServletContext getServletContext() Returns the current ServletContext object.
HttpSession getSession() Returns the current HttpSession object.



Advertisement




An example of using the pageContext object




SettingAttributes.jsp
<html>
<head>	
<title>JSP pageContext</title>
</head>

<body>

<%  
pageContext.setAttribute("Segment", "IT", pageContext.PAGE_SCOPE);
pageContext.setAttribute("Topic", "JSP", pageContext.REQUEST_SCOPE);
pageContext.setAttribute("Website", "DecodeJava", pageContext.SESSION_SCOPE);
pageContext.setAttribute("Year", "2018", pageContext.APPLICATION_SCOPE);
%>

<% 
pageContext.forward("DataDisplay.jsp");
%>

</body>
</html>



  • Due to call to the forward() method of JSP's inbuilt pageContext object the ServletRequest is forwarded to another JSP page GettingAttributes.jsp


  • GettingAttributes.jsp
    <html>
    <head>	
    <title>Using JSP pageContext object</title>
    </head>
    
    <body>
    
    
    Accessing the value of attribute "Segment", set in the page scope:
    <% out.println(pageContext.getAttribute("Segment", pageContext.PAGE_SCOPE)); %>
    <br/>
    <br/>
    Accessing the value of attribute "Topic", set in request scope : 
    <% out.println(pageContext.getAttribute("Topic", pageContext.REQUEST_SCOPE)); %>
    <br/>
    <br/>
    Accessing the value of attribute "Website", set in session scope : 
    <% out.println(pageContext.getAttribute("Website", pageContext.SESSION_SCOPE)); %>
    <br/>
    <br/>
    Accessing the value of attribute "Year", set in application scope : 
    <% out.println(pageContext.getAttribute("Year", pageContext.APPLICATION_SCOPE)); %>
    
    </body>
    </html>



    Finally, on running SettingAttributes.jspM all the attributes set in the request, session and application scope will be read and their associated values will be displayed.






    Note :


    Value of the attribute Segment set in the page scope on the web page SettingAttributes.jsp is showing null because we are reading the value of this attribute in a different JSP web page i.e. GettingAttributes.jsp and not in the page in they were set.




    Please share this article -




    < Prev
    Next >
    < JSP exception object
    JSP Built-in Methods >



    Advertisement

    Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement



    Notifications



    Please check our latest addition

    C#, PYTHON and DJANGO


    Advertisement