• Post Reply Bookmark Topic Watch Topic
  • New Topic

[ Difference b/w request.getAttribute and request.getParameter

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question:
Why are the (name,value) pairs retrieved via
request.getParameter different from the ones
fetched using request.getAttribute ? Are they
in different namespaces ?
I also want to remove (name,value) pairs from
the request object before forwarding.
Thanks
Pho
 
Rishi Singh
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well one should be using a request.getParameter() just after the form submission, but request .getAttribute can be called at the end jst to check the value of the attribute..I mean if there are 3 pages across a transaction, then on submission of each page one has to call request.getParameter(), but suppose at the last page before the commit if u want to check all the values of all the attributes across the three pages then u will be using request.getAttribute, similarly for removing one needs to use removeAttribute()
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pho
The getParameter( ) method is used to get the value of the request items sent from a form to your servlet. Request parameters are different from attributes.
An attribute can come from the container or you can use the setAttribute() method to put your own values in.
As far as I know there is no easy way to remove an item from the request, I know you can add to it but I'm not sure about removing things.

Rishi
I'm not sure what you mean by you would access the request's attributes after the last page to find all of the form values submitted. Anything associated with a request goes away after the request has been handled. If you have multiple pages in a form then you'll need to cache them in a database or in session variables they wont remain in the request.
 
sandy gupta
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
request.getParameter is used when the method of submission is a get/post and the attributes are coming in the form of form parameters. On the other hand, request.getAttribute is used if u have explicitly set attributes in the request object so as to use them at a later stage.
And guys, there is a method called request.removeAttribute which can be used to remove an attribute from the request.
Genarally its a good practice not to add a whole lot to the request as 1) u lose it when u redirect as a new request object is created
2) the page loading is affected as the request object gets heavier
HTH
Sahil

[ May 24, 2002: Message edited by: sandy ind ]
 
Mayer Salzer
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To throw in my own 2 cents on this topic . .
Attributes are set explicitly (with the setAttribute() method). This is nice since it allows you to add attributes to a request object at any point and then forward it to another servlet or jsp. Parameters on the other hand come from posted form variables AND from the query string (example: http: //hostname/myServlet?testing=123, will return a value of '123' when you call request.getParameter("testing"); ). This means that similar to attributes, you can intercept a request and add parameters to it, by appending a query-string, and then redirecting it to another servlet (however I'm not sure whether you'd then lose any parameters that were in the orginal query-string).
Another significant difference between the 2 is that parameters can only be 'String' objects (and are returned as such from the getParameter method). Whereas attributes can be any object (and are returned as 'Object' objects which you then cast back to its original type).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!