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 in the jsp’s we have page scope also.

Request scope

  1. In servlet we can maintain this scope by using ServletRequest object,but in the jsp’s we can maintain it by using request implicit object.
  2. The information stored in request scope is by default available for all the components which are processing that request.
  3. Request scope will start at the time of request object creation (i.e just before starting service ()) and ends at the time of request object destruction(i.e just after completing service()).
  4. We can perform attribute management in request scope by using the following methods of servlet request interface.
  5. public void setAttribute (string name, object value)
    public object getAttribute(string name)
    public void removeAttribute(string name)
    public enumeration getAttributenames()

session scope

  1. In servlet we can maintain session scope by using HttpSession object but in jsp’s we can maintain by using session implicit object.
  2. The information stored in session scope is by default available for all the components which are participating in that session.
  3. Session scope will be started at the time of session obj is creation and ends once session expires.
  4. By using the following methods of Http session interface we can perform attribute management in session scope.
  5. setAttribute(string name, object value)
    getAttribute(string name)
    removeAttribute(string name)
    getAttributeNames()

Application scope

  1. We can maintain application scope in servlet by using servlet context object but in jsp’s we can maintain by using application implicit object.
  2. The information stored in the application scope is by default available for entire web application.
  3. Application scope will start at the time of creation (i.e at the time application deployment and ends at the time of context object destruction(i.e at the time of application undeployment)
  4. Servlet context interface defines the following methods to perform attribute management in application scope
  5. setAttribute(string name, object value)
    getAttribut(string name)
    removeAttribute(string name)
    getAttribute names()

Page scope

  1. This scope is application only for jsp’s but not for servlets.
  2. We can maintain this scope by using pagecontext implicit object.
  3. The information stored in page scope is by default available within the “translation unit”
  4. page scope is most commonly used scope in customer tags to share into between tag handle classes.
  5. pagecontext class defines the following methods to perform attribute management in page scope.
  6. public void setAttribute (string name,object value).
    public Object getAttribute(string name)
    public void removeAttribute(string name).

Attribute management in any scope by PageContext

PageContext class defines the following methods for this

  1. public void setAttribute(string name,object value,int scope)
    1. Scope—->PageContext.APPLICATION_SCOPE—->4
    2. PageContext.SESSION_SCOPE—->3
    3. PageContext.REQUEST_SCOPE—->2
    4. PageContext.PAGE_SCOPE—->1
  2. public object getAttribute(string name,int scope)
  3. public void setAttribute(string name,int scope)
  4. public Enumeration getAttribute names in scope(int scope)

PageContext class defines the following extra specific method

public object find Attribute (string name)

jsp engine searches first in the page scope for the required attribute. If it is not available then it will search request,session and application scopes respectively .If it is available then it returns that object and if it is not available then it returns null.