This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes request.getAttribute(param) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "request.getAttribute(param)" Watch "request.getAttribute(param)" New topic
Author

request.getAttribute(param)

Nadeem Ahmad
Greenhorn

Joined: Oct 20, 2003
Posts: 28
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

Joined: Nov 06, 2000
Posts: 1510
Yes, if you added the attribute to the request.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Parham Emami
Greenhorn

Joined: Jul 11, 2003
Posts: 15
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

Joined: Nov 26, 2003
Posts: 382
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().


Ever Existing, Ever Conscious, Ever-new Bliss
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

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

Joined: Oct 20, 2003
Posts: 28
Thank you Very Much Standard
Now it is clear
 
jQuery in Action, 2nd edition
 
subject: request.getAttribute(param)