Setting Attributes

Setting Atributes :

Setting attributes is required, when we want to share the dynamic data in different components of web application.
Attribute is an object, which is set on in one of the three servlet scopes:

  • Request Scope  : It saves the data in the form of key-value pairs in the ServletRequest object of the Servlet to pass to another Servlet. Data can be added using setAttribute(key,value) method over ServletRequest object and can be fetched using getAttribute(key).
  • Session Scope  : Using and maintaining sessions we can store some of the data in form of key-value pairs.
  • Context Scope :  It saves the data in the form of key-value pairs in the ServletContext.
    getServletContext() can be used to get object of ServletContext.
    Data can be added using setAttribute(key,value) method over ServletContext object and can be fetched using getAttribute(key).
    In this, data available in the ServletContext is available to all the Servlets.

Three attribute specific methods are:

Method
Purpose
void setAttribute(String name,Object object)
Returns the attribute for the specified name.
Object getAttribute(String name)
Sets the given object in the application scope.
void removeAttribute(String name)
Removes the attribute with the given name from the servlet context.

Leave a Reply

Your email address will not be published. Required fields are marked *