• Loading
    • Attribute Scopes & Servlet Chaining

      Learning Objectives
      After completing this session, you will be able to:
      • Describe attribute scopes
      • Explain Servlet Chaining


      Attributes
      • Attributes allow data to be stored and implemented in inter servlet communication.
      • Attributes can also be used with the session mechanism to preserve data between requests.
      • The main methods associated with attribute manipulation are:
        o setAttribute
        o getAttribute
      • Attributes are stored using setAttribute as name and value pairs with the signature:
        o setAttribute(String name, Object value);
        For example setAttribute(“count”,new Integer(99));
      • Attributes are retrieved using getAttribute:
        Integer count= (Integer) session.getAttribute("count");



      Servlet Chain


      Output of one servlet is piped into a second servlet. The output of the second servlet could be piped into a third servlet, and so on. The last servlet in the chain returns the output to the Web browser.
      Example
      Code:
      Ways to achieve Servlet Chaining: 
      Include: RequestDispatcher rd = req.getRequestDispatcher("Servlet2"); rd.include(req, resp); 
      Forward: RequestDispatcher rd = req.getRequestDispatcher("Servlet3"); rd.forward(req, resp);

      Try It Out


      Problem Statement:
      Create a servlet named FirstServlet and set some attributes in all the possible scopes. Now transfer the control to another servlet ’SecondServlet’and display the attributes on the browser.

      Code:

      Code:
      import java.io.IOException; 
      import javax.servlet.RequestDispatcher; 
      import javax.servlet.ServletContext; 
      import javax.servlet.ServletException;
      import javax.servlet.http.*; 
      public class FirstServlet extends HttpServlet { 
      public void doGet(HttpServletRequest req, HttpServletResponse res) 
      throws ServletException, IOException { 
      req.setAttribute("reqAttr", "this is request Attribute"); 
      HttpSession sess=req.getSession(); 
      sess.setAttribute("sessAttr", "this is session Attribute"); 
      ServletContext cont=getServletContext(); 
      cont.setAttribute("contAttr", "this is context Attribute"); 
      RequestDispatcher reqDispatcher=req.getRequestDispatcher("/second.do"); 
      reqDispatcher.forward(req,res); 
      } 
      }
      Code:
      public class SecondServlet extends HttpServlet { 
      public void doGet(HttpServletRequest req, HttpServletResponse res) 
      throws ServletException, IOException { 
      String reqAttr=(String)req.getAttribute("reqAttr"); 
      HttpSession sess=req.getSession(); 
      String sessAttr=(String)sess.getAttribute("sessAttr"); 
      ServletContext cont=getServletContext(); 
      String contAttr=(String)cont.getAttribute("contAttr"); 
      res.setContentType("text/html"); 
      PrintWriter out=res.getWriter(); 
      out.println("<html><head><title>SecondServlet</title></head>"); 
      out.print("<body><h1>The request Scope Attribute value is : "+reqAttr+"</h1><br>"); 
      out.print("<h1>The session Scope Attribute value is : "+sessAttr+"</h1><br>"); 
      out.print("<h1>The session Scope Attribute value is : "+contAttr+"</h1><br>"); 
      out.print("</body></html>"); 
      } 
      }

      How It Works:

      • Web.xml maps the logical path to the physical location of the servlets
      • The attributes are set in the various scopes in the first servlet.
      • PrintWriter object is used to display the response in browser in the second servlet


      Summary
      • Attributes allow data to be stored and used in inter servlet communication.
      • Attributes can also be used with the session mechanism to preserve data between requests.
      • Output of one servlet is piped into a second servlet. The output of the second servlet could be piped into a third servlet, and so on. The last servlet in the chain returns the output to the Web browser.


      Test your Understanding
      • What are the different attribute scopes?
      • What is Servlet Chaining



    Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.