java2db

Request attributes in servlet with an example ( Attribute scope )



A Servlet can communicate with another servlet or JSP by using RequestDispatcher object (which is commonly known as servlet chaining). To pass additional information between source and destination servlets , we can use request attributes .
The sendRedirect
Request attributes in servlet with an example Request attributes are the name (String) and value (Object) pairs.

Request attributes in servlet with an example In servlet chaining , If we create request attribute in the source servlet (Ex : SetAttributeServlet) , it can be visible and accessible in the destination servlet/JSP (Ex : GetAttributeServlet).

Request attributes in servlet with an example If any servlet (Ex: IndividualTestServlet) not participate in the servlet chaining , it cannot access request attributes even though it resides in the same web application.

Request attributes in servlet with an example Request attribute allocates memory in the ServletRequest object.

Request attributes in servlet with an example Request attributes are specific to each and every request (request scope).

 

Request attributes in servlet with an example Create request attributes :

syntax:

<HttpServletRequest_Obj> . setAttribute(“<Attribute_Name>”,”<Attribute_Value>”) ;

Example:

request.setAttribute(“name”,”java2db”);

Attribute value must be an object . But we passed a String value , so it applies auto boxing .

 

Request attributes in servlet with an exampleModify request attributes  :

Syntax :

<HttpServletRequest_Obj> . setAttribute(“<Attribute_Name>”,”<New_Attribute_Value>”) ;

Example :

request.setAttribute(“name”,”java2db.com”);

 

Request attributes in servlet with an example Remove request attributes :

Syntax :

<HttpServletRequest_Obj> . removeAttribute(“<Attribute_Name>”) ;

Example :

request.removeAttribute(“name”,”java2db.com”);

 

Request attributes in servlet with an example Access  request attributes :

Syntax :

(Type)<HttpServletRequest_Obj> . getAttribute(“<Attribute_Name>”) ;

Example :

String name = (String)request.getAttribute(“name”);

 

Request attributes in servlet with an example Complete example for request attributes :

Request attributes in servlet with an example Directory Structure :

The sendRedirect

Request attributes in servlet with an example index.jsp

Request attributes in servlet with an example SetAttrivbuteServlet.java

Request attributes in servlet with an example GetAttributeServlet .java

Request attributes in servlet with an example IndividualTestServlet .java

Request attributes in servlet with an example web.xml

Execution result :

Test URL :

http://localhost:8080/RequestAttributes/index.jsp

Change the IP address and Port according to your server location IP and Port.

 

 

Enter the details and click Submit button.

The sendRedirect

 

The response will be the below window.
The sendRedirect

 

 

Now test the individual servlet which is not participating in the servlet chaining with it’s (http://localhost:8080/RequestAttributes/indServlet) URL   . The response comes with null values  (Because there is no call from any servlet with RequestDispatcher object).
The sendRedirect




Related Posts :
Kill or invalidate session using session id ( concurrent login )
Servlet Filter ( Request and Response filters in servlets ) example
URL rewriting in servlets ( Servlet session tracking )
Servlet Cookies ( Servlets session tracking with cookie )
Hidden form field in servlets ( Servlet session tracking )
Session attributes in servlet with example (Attribute scope)
ServletConext Attributes in servlet ( Attribute scope )
The sendRedirect (servlets are in different web applications)
   [will not be published]



^ <