• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

RequestDispatcher and setAttribute

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is 100% correct.
 
Where all the women are strong, all the men are good looking and all the tiny ads are above average:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic