wood burning stoves 2.0*
The moose likes Servlets and the fly likes  RequestDispatcher and setAttribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark " RequestDispatcher and setAttribute" Watch " RequestDispatcher and setAttribute" New topic
Author

RequestDispatcher and setAttribute

Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
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
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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.
Stefan Geelen
Ranch Hand

Joined: Nov 19, 2001
Posts: 49
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
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

That is 100% correct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher and setAttribute
 
Similar Threads
How to get to Servlet API documentation
Class diagram of Servlet API
J2EE API
Null object when calling session context object
jspInit() or jsp_init()?