This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
In the online manual http://java.sun.com/products/servlet/2.2/javadoc/index.html I can read: This (setAttrbibute) method is most often used in conjunction with RequestDispatcher. On the page http://java.sun.com/products/servlet/2.2/javadoc/index.html I can read (concerning the RequestDispatcher include method): <bold>The request and response parameters must be the same objects as were passed to the calling servlet's service method. </bold> This seems to me a contradiction. Following code will not work: ... request.setAttribute("id","prog"); request.setAttribute("action","add"); RequestDispatcher rd = request.getRequestDispatcher("counter"); rd.include(request,response); ... The counter servlet will NOT have in its request object the 2 added parameters. Do I miss something ? Stefan
I think you're confused by parameters to the request dispatcher and any parameter (or attributes) on the request object itself?
You can use setAttribute() to add numerous other attributes and then forward it to another page. These newly added attributes WILL be available in the next page (or servlet).
When they say 'must be the same objects' they're not meaning 'the object in the request object's attribute space (or parameter space) must be the same', they mean that you cannot use a 'different' request object to obtain a request dispatcher. I'm not sure where they think we'd obtain a different request object, but I guess they're just making it clear that the request object you use must be the same one passed into the service method by the container.
So just to be clear, the code you provided WILL work, but the request will have those two addition in the 'attribute' space and not the parameter space.
Joined: Nov 19, 2001
Mike, thx for the quick 'response' on my 'request' - . I still have to try but I suppose the second servlet (the one that is called) can get the attributes with the request.getAttribute() method ? Thx again for your information. Stefan