JSP Page Scope

Introduction: 
                                              
In Servlets we have the following 3 scopes for storing information in the form of attributes.

1.    Request Scope
2.     Session Scope
3.     Application Scope
In addition to these 3 scopes we have page scope is available  in jsp’s.


                                                Page Scope

1.    This scope is applicable only for jsp’s but not for servlet.
2.     This scope is maintained by pageContext implicit object.
3.     An object with this scope can be accessed by invoking the getAttribute ( ) method on the implicit page Context object. The object is created and destroyed for each client request to the page.
4.    This is the default scope for objects used with the jsp:useBean action.
5.     The JSP object can be accessed only from within the same page where it was created. JSP implicit objects out, exception, response, pageContext, config and page have page scope.
6.     Page scope in the most commonly used scope to share information between  tag handler class.
7.    PageContext class defines the following methods to perform attribute management in page scope.

Methods:

1.    public void setAttribute (String name, Object value)
2.     public void getAttribute (String name)
3.     public void remove Attribute (String name);
Attribute Management in any scope:
PageContext class defines the following methods to perform attribute management in any scope.
      public void setAttribute (string name, object value, int scope)

Here scope value must be 1 to 4

PageContext.PAGE-SCOPE                 =    1
PageContext.REQUEST-SCOPE          =    2
PageContext.SESSION-SCOPE            =    3
PageContext.APPLICATION-SCOPE  =    4
Example:
setAttribute(“uname”,”chamu0001”,1)
2.    public Object getAttribute (String name, int scope)
3.      public void removeAttribute (String name, int scope)
4.     public Enumeration getAttribute NamesInScope(int scope)
5.     public Object findAttribute (String name)
6.    First it will search in page scope for desired attribute, if it is not available then it will search in request scope followed by session scope and application scope.
7.     page – request – session – application.
Example: UsingBeanScopePage.java

package Mybean;
public class UsingBeanScopePage{
  private static int counter = 0;
  public void setCounter(int counter) {
    this.counter = counter;
  }
  public int getCounter(){
    return counter;
  }
}
 Index.jsp

<html>
  <head>
    <title>Using Beans and Page Scope</title>
  </head>
  <body>
    <h1>Using Beans and Page Scope</h1>
    <jsp:useBean id="pageScopeBean" class="Mybean.UsingBeanScopePage" scope="page" />
    <%
    pageScopeBean.setCounter(pageScopeBean.getCounter() + 1);
    %>
        Counter value is <%= pageScopeBean.getCounter() %>
  </body>
</html>
  Output:
 Using Beans and Page Scope
 couter value is 3

Thanks For Making This Possible! Kindly Bookmark and Share it.

Technorati Digg This Stumble Stumble Facebook Twitter

No comments:

Post a Comment