In our last posts, we have learned
JSP implicit objects REQUEST with example,
JSP implicit objects OUT with example,
JSP implicit object RESPONSE with example,
JSP Implicit Object CONFIG with example ,
JSP Implicit Object APPLICATION with example ,
JSP Implicit object session with example
The pageContext object is an instance of a javax.servlet.jsp.PageContext object.
The pageContext objects has special access to manage attributes from following scopes :
1) Application Level – Scope: APPLICATION_SCOPE
2) HTTP Request – Scope: REQUEST_SCOPE
3) HTTP Session – Scope: SESSION_SCOPE
4) JSP Page – Scope: PAGE_SCOPE
Methods of pageContext implicit object :
1) findAttribute(java.lang.String name)
2) getAttribute (java.lang.String name)
3) getAttribute (java.lang.String name, int Scope)
4) removeAttribute(String AttributeName, int Scope)
5) setAttribute(String AttributeName, Object AttributeValue, int Scope)
6) setAttribute(String AttributeName, Object AttributeValue)
As mentioned above, there are 2 flavors of setAttribute method. If Scope is not passed as an attribute, then the attribute is set to current JSP page i.e. by default the scope is PAGE_SCOPE.
There is a slight difference between the methods findAttribute(java.lang.String name), getAttribute (java.lang.String name), getAttribute (java.lang.String name, int Scope). Click here to know the difference
Example:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% pageContext.setAttribute("Interest Rate", 10, PageContext.SESSION_SCOPE); pageContext.setAttribute("Loan Amount", 10000); // Searches for the named attribute in all the 4 scopes i.e. <b>page, request, session (if valid), // and application scope(s)</b> pageContext.findAttribute("Loan Amount"); // Return the object associated with the name in the <b>page scope</b> only pageContext.getAttribute("Loan Amount"); // Return the object associated with the name in the specified scope pageContext.getAttribute("Loan Amount", PageContext.REQUEST_SCOPE); %> </body> </html>
No comments:
Post a Comment