Recipe 16.9 Setting Request Attributes in Servlets


Problem

You want to use a servlet to store an attribute in a request.

Solution

Use the javax.servlet.ServletRequest.setAttribute( ) method.

Discussion

The ServletRequest.setAttribute( ) method is often used in code that dynamically forwards requests or includes content with a javax.servlet.RequestDispatcher .

Web applications that use RequestDispatcher s to share requests between web components can communicate between these components using request attributes. Both the recipient of the RequestDispatcher.forward( ) method and the included file or page involved with the RequestDispatcher.include( ) method have access to the original or enclosing request. Therefore, these web components can also access any object attributes that are stored in those requests.

The servlet in Example 16-10 creates an instance of a ContextObject , stores some information in the object by calling its put( ) method, and then places the object in the HttpServletRequest under the name "com.jspservletcookbook.ContextObject." The servlet then uses a RequestDispatcher to forward the request (including the attribute) and response to the servlet path /displayAttr . The web component mapped to that servlet path now has access to the previously created request attribute.

Example 16-10. Binding an object to a request
 package com.jspservletcookbook;            import javax.servlet.*; import javax.servlet.http.*; public class RequestBinder extends HttpServlet {   public void doGet(HttpServletRequest request,      HttpServletResponse response) throws ServletException,       java.io.IOException {  //bind an object to the request       ContextObject contextObj = new ContextObject( );       contextObj.put( request.getRemoteAddr( ), ""+new java.util.Date( ));       request.setAttribute(         "com.jspservletcookbook.ContextObject",contextObj );            //use RequestDispatcher to forward request to another servlet       // mapped to the servlet path '/displayAttr'       RequestDispatcher dispatcher = request.getRequestDispatcher(         "/displayAttr");       dispatcher.forward(request,response);  } //doGet } 

Example 16-11 shows the servlet that receives the forwarded request. The RequestDisplay servlet is mapped in web.xml to the /displayAttr servlet path. This servlet gets the request attribute from the HttpServletRequest object by calling getAttribute( ) with the attribute name: com.jspservletcookbook.ContextObject . Since the return value of getAttribute( ) is typed to Object , the code must cast the result to ContextObject .

Example 16-11. The target of RequestDispatcher.forward has access to the request attribute
 package com.jspservletcookbook;            import javax.servlet.*; import javax.servlet.http.*; public class RequestDisplay extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws  ServletException, java.io.IOException {  ContextObject obj = (ContextObject) request.getAttribute(         "com.jspservletcookbook.RequestObject");  response.setContentType("text/html");     java.io.PrintWriter out = response.getWriter( );     out.println(         "<html><head><title>Request Attribute</title></head><body>");     out.println("<h2>Request attribute values</h2>");  //display the keys of the java.util.Map stored in the request object     //attribute     if (obj != null)         out.println( obj.getValues( ) );  out.println("</body></html>");        } //end doGet } 

Make sure to check whether the ServletRequest.getAttribute( ) return value is null before calling any of the object attribute's methods . The getAttribute( ) method returns null if the request does not contain an attribute of the specified name.

See Also

Recipe 16.1-Recipe 16.4 on handling ServletContext attributes in servlets and JSPs; Recipe 16.5-Recipe 16.8 on handling session attributes in servlets and JSPs; Recipe 16.10 on setting request attributes in JSPs; Recipe 16.11 and Recipe 16.12 on accessing or removing request attributes in servlets and JSPs; the Javadoc for javax.servlet. ServletRequestAttributeListener : http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequestAttributeListener.html.



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net