Type of JSP scopes

There are four types of JSP scopes

  1. Page scope
  2. Request scope
  3. Session scope
  4. Application scope

1. Page scope

  • Data from page can be accessed within the same JSP only
  • Data can be stored directly in request, session and application using setAttribute but
  • To save data in page scope you have to use implicit object pageContext
  • It is of java.lang.Object type
  • Methods of PageContext to manage attributes and its values
    • void setAttribute(String attrName, Object attrValue)
      void setAttribute(String attrName, Object attrValue, int scope)
      Object getAttribute(String attrName)
      Object getAttribute(String attrName, int scope)
      Object findAttribute(String attrName)
      void removeAttribute(String attrName, int scope)
      Enumeration getAttributeNamesInScope(int scope)

2. 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

3. 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

4. Application 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