• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

request.getAttribute(param)

 
Nadeem Ahmad
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a text box in my HTML page named txtUserName.
after submitting the i get the value by request getParameter.
But if I use
String strUserName=(String)request.getAttribute("txtUserName");
Then it return only null;
can I get the value by using request.getAttribut("txtUserName");
if not then plz let me know the Difference
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you added the attribute to the request.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to use getAttribute to get a parameter? Especially when you know that getParameter works just fine?
They are completely separate concepts.
 
Parham Emami
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there are three ways to get the values of html elements:

1)(first and the easiest)the best way to take out the value of the elements of your html form is to use {request.getParameter("htmlElementName")}
and pass it through an {String},
2)and so if you have many {txtInputs} and it's so hard to handle them one by one you should name them in logical proper names {like:txtInputName,txtInputFamily,...} and then you can use the {request.getAttributeNames()} to take out the parameters(by casting) and pass them through an Enumeration object to navigate them.
3)and you can also use {request.getAttribute()} method and use its value by casting it to string
but the 1 and 2 are the best way to handle the parameters of an html elements.
i'll be glad if i could help you a little: Parham.
[ February 05, 2004: Message edited by: Parham Emami ]
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Parham Emami:
there are three ways to get the values of html elements:

1)(first and the easiest)the best way to take out the value of the elements of your html form is to use {request.getParameter("htmlElementName")}
and pass it through an {String},
2)and so if you have many {txtInputs} and it's so hard to handle them one by one you should name them in logical proper names {like:txtInputName,txtInputFamily,...} and then you can use the {request.getAttributeName()} to take out the parameters and pass them through an Enumeration object to navigate them.
3)and you can also use {request.getAttribute()} method and use its value by casting it to string
but the 1 and 2 are the best way to handle the parameters of an html elements.
i'll be glad if i could help you a little: Parham.

Not true. You cannot use 2 & 3 to get the parameters that get submitted (via Get/Post) from a web page. This is because the parameters are not attributes of the request object.
If you want an enumerated list of parameters you will need to use getParameterNames().
Nadeem, getAttribute() will return the value of an attribute that is set in the request object via the setAttribute() method. The parameters that get submitted (in the querystring) cannot be accessed via getAttribute(). You have to use getParameter().
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sadanand is correct; attributes and parameters are separate concepts and any of the 'attribute' methods cannot be used to obtain request parameters.
[ February 04, 2004: Message edited by: Bear Bibeault ]
 
Nadeem Ahmad
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Very Much Standard
Now it is clear
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic