Monday, 27 June 2016

How to access values and resources and to set object attributes within 3 scopes ?


Request scope

Methods in ServletRequest interface
  • Object getAttribute(String name)
    Returns the value of an attribute from request or null if attribute is not present
  • Enumeration getAttributeNames()
    Returns an Enumeration containing the names of the attributes
  • ServletInputStream getInputStream()
    Retrieves the body of the request as binary data
  • setAttribute(String name, Object o)
    Stores an attribute in current request
    Most often used in conjunction with RequestDispatcher
  • removeAttribute(String name)
    Removes an attribute from current request
    Not needed as attributes only persist as long as the request is being handled.


Session scope
Methods in HttpSession interface 
Works same way as ServletRequest but associated to Session
  • Object getAttribute(String name)
  • Enumeration getAttributeNames()
  • setAttribute(String name, Object o)
  • removeAttribute(String name)



Context scope
Methods in ServletContext interface
Works same way as ServletRequest but associated to the entire Application
  • Object getAttribute(String name)
  • Enumeration getAttributeNames()
  • setAttribute(String name, Object o)
  • removeAttribute(String name)

ServletContext also provides direct access to the hierarchy of static content documents that are part of the web application, including HTML, GIF, and JPEG files.
  • getResource(String resource)
    Example : getResource("/index.jsp") will return JSP source code
  • getResourceAsStream(String resource)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.