pagecontext implicit object in jsp

By | March 30, 2020

Introduction of pageContext implicit Object in jsp-: pagecontext implicit object is an instance of javax.servlet.jsp.PageContext object. The pageContext object is used to get, set and remove attributes from the scope.The following scopes are given here ..

  • page : PAGE_CONTEXT
  • request : REQUEST_CONTEXT
  • session : SESSION_CONTEXT
  • application : APPLICATION_CONTEXT

pageContext implicit Object Methods in JSP

setAttribute (String name, Object value, int scope)-: setAttribute () method sets the attribute to the given scope. Here the attribute name, its value and scope are given. for example

<% pageContext.setAttribute(“message”, “Hello”, PageContext.APPLICATION_SCOPE); pageContext.setAttribute(“message”, “netnic”, PageContext.SESSION_SCOPE); %>

getAttribute (String name, int scope)-: getAttribute () method returns the value of the attribute from the given scope and attribute name to the object. If the given scope or attribute name is invalid, ‘null’ returns. For example

<%
   pageContext.setAttribute("message", "Hello", PageContext.APPLICATION_SCOPE);     
   pageContext.setAttribute("message", "netnic", PageContext.SESSION_SCOPE); 
   out.print(pageContext.getAttribute("message", PageContext.APPLICATION_SCOPE) + " "); 
   out.print(pageContext.getAttribute("message", PageContext.SESSION_SCOPE));
 %>

removeAttribute (String name, int scope)-: removeAttribute () method removes the attribute from the given scope and attribute name. For example

<%
 pageContext.setAttribute("message", "Hello", PageContext.APPLICATION_SCOPE); 
 pageContext.setAttribute("message", "netnic", PageContext.SESSION_SCOPE); 
 pageContext.removeAttribute("message", PageContext.SESSION_SCOPE);
 %>

findAttribute (String name)-: findAttribute () method returns the value from the attribute name given on all the scope. findAttribute () and getAttribute () are both similar. But findAttribute () this method looks for the value of the attribute name on all the scope and getAttribute () finds the value from the given scope and attribute name.For example

<% 
    pageContext.setAttribute("message", "Hello", PageContext.APPLICATION_SCOPE); 
    pageContext.setAttribute("message", "netnic", PageContext.SESSION_SCOPE); 
    out.print(pageContext.findAttribute("message")); 
%>
More about jsp implicit object

 application implicit Object in jsp 

 config implicit Object in jsp 

 out implicit Object in jsp 

 response implicit Object in jsp 

 implicit objects in jsp 

 Directives in jsp 

 Java server Pages Tags 

 life cycle of jsp 

 What is java server pages 

More about programming language

Category: jsp

Leave a Reply

Your email address will not be published. Required fields are marked *