Scope is used to specify the lifespan of an object. Any data stored in object is persisted throughout the scope of object and this data can be accessed any time within the scope as per requirement. Object that are bound to a particular scope called scoped object.

Data can be stored in scope as an attribute, attributes are name-value pair. name of attribute is type of String and value of attribute is type of Object. These are read and write attributes.

Methods for using scope object

    void setAttribute(String attrName, Object attrValue)
    Object getAttribute(String attrName)
    void removeAttribute(String attrName)
    Enumeration getAttributeNames()

Type of Servlet Scopes

There are mainly three types of servlet scopes

  1. Request scope
  2. Session scope
  3. Context scope

1. Request scope

  • Data stored in HttpServletRequest will be available until request object destroyed
  • Scope of such data called request scope
  • Request data can be accessed by single user within same request and before sending the response

2. Session scope

  • Data stored in HttpSession object will be available until session persists
  • Scope of such data called session scope
  • Session data can be accessed by single user across multiple requests
  • User should belong to same session

3. Context scope

  • Data stored in ServletContext will be available throughout the life of web application
  • Scope of such data is called context scope
  • Context data can be accessed by multiple users across multiple requests
Footer with Map