Sevlet Attributes are the functionality given to have communication between servlet to servlet/jsp or jsp to servlet/jsp. Attributes can be used with the request object or session object, both are having different scopes of communication and stability for a web application. These attributes are implemented through two methods i.e setAttribute() and getAttribute(). In servlet we have basically two major scopes of communication i.e request and session. Both the methods are defined in the javax.servlet.ServletRequest and javax.servlet.http.HttpSession interfaces for request and session scope respectively.

Design of getAttributes()
getAttributes() is capable of storing information in key and value format. It is internally implemented using map concept.

setAttribute(String, Object)

key is represented in the form of string and must be unique.
value can be any object.

We can set the object using setAttribute() with the following two scope,
1. Request Attributes.
2. Session Attributes.

Request Attributes
Through the request attribute we are storing the value ( any object ) in the request object using the setAttribute(). It is used by the servlet for communicating with the other servlet and Jsp.

public void service(HttpServletRequest req, HttpServletResponse res){

         .......
   List<String> list= new ArrayList<String>();
   list.add("JAVA");
   list.add("CPP");
   req.setAttribute("NAME", list);

}

In the above example we are calling setAttribute() with the HttpServletRequest object and setting the value as a list object with the name as String value "NAME"

We can get the list object in the another servlet/jsp with the use of getAttribute(String) with the request object only by passing the string value which is used as a key in the setAttribute(). 

Note: getAttribute() returns the Object class object which must be type casted in the required form to access the value stored in the value of the setAttribute().

Session Attribute
It is same as the Request Attribute with only difference of storing procedure. Previously we stored list object in the request object , now in session Attribute we are storing the same list object in the HttpSession object. 

public void service(HttpServletRequest req, HttpServletResponse res){

         .......
   List<String> list= new ArrayList<String>();
   list.add("JAVA");
   list.add("CPP");
   HttpSession session=req.getSession();
   session.setAttribute("NAME", list);

}

In the above example we are calling the setAttribute() with the session object which internally storing the list object in the session object. 

We can get the value stored in the list from the session object by calling the getAttribute(String) with the session object and passing the String value which is used as a key in the getAttribute() method. 

public void service(HttpServletRequest req, HttpServletResponse res){

         .......
   
   HttpSession session=req.getSession();
   List<String> list=(ArrayList<String>)session.getAttribute("NAME");

}

As getAttribute() returns the Object class object so it must be type casted to the following object type in which the typed object is stored. 


0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic