What is difference between getAttribute() and getParameter()?

Showing Answers 1 - 15 of 15 Answers

shiv shankar

  • Nov 23rd, 2005
 

getAttribute() method always returns you an object , So when you do like <%= getAttribute("....") %>This will print you some junk value on the browser . To print some meaning full we need to type cast to String or to some required class.getParameter() methos always returns an string .

  Was this answer useful?  Yes

shiv shankar

  • Nov 23rd, 2005
 

getAttribute() method always returns you an object , So when you do like <%= getAttribute("....") %>This will print you some junk value on the browser . To print some meaning full we need to type cast to String or to some required class.getParameter() methos always returns an string .

  Was this answer useful?  Yes

Amit Mahabal

  • Dec 4th, 2005
 

The getAttribute() method always returns you an object , class.getParameter() method always returns an string .

  Was this answer useful?  Yes

nitin

  • Dec 16th, 2005
 

getAttribute() returns an object so when ever we use a getAttribute() we have to cast the return type that is we have to convert it to the required type like string or int.getParameter always returns a string.

one more difference is where this 2 methods are used...getAttribute is used to get the values from the session and getParameter is used to get the values from the form.(html or a jsp file)

first one is getAttribute returns object and getParamter returns String

Second one is getAttribute is used to call only that are included in the request object and getParamter is used for only form attributes like text box ,drop down box etc and values of url appended.

raghavendra

  • Feb 14th, 2006
 

request.getAttribute("name"); it will return null when "name" is not present in request object.

request.getParameter("name"); it will throws an exception when "name" is not present in the request object.

  Was this answer useful?  Yes

Ken

  • Mar 2nd, 2006
 

I thnk none of your replies provides the right answer to this question ...

I shall try and explain in my words & hopefully you'll understand...

The basic difference between getAttribute() and getParameter() is that the first method extracts a (serialized) Java bean object and the other provides a string value. For both cases a name is given so that its value (be it string or a java bean) can be looked up and extracted.

Now scopewise/ based on applicability their differences are enormous. A Java bean can be of 4 scopes (page, request, session and application). Thus getAttribute() can be used to get Java bean objects of any of the above 4 scopes.

I'm not going to get into the specifics of scopes here as its a different interview question subject, but I want to stress the following: The getParameter() can only access in the request (POST/GET) scope. This scope is universal in all server languages incl. J2EE. However, since this isnt OOD based we cannot objectify parameter data, nor can we add any business intelligence into it. Thats where J2EE's based Java beans (not talking EJB here) come into play and add a new protocol of how data can be viewed/edited at the web application with the inherited flexibility in its OOD.

Jayant

  • Jun 7th, 2006
 

getAttribute("setname")- this methods return only respective set object if setname is not found then return always null.

This methods retired objects using session,  and response object.

Exp - response.setAttribute("FirstName", "Jayant"); // second argument always object type.

FirstName fetch from request like

String firstName =request.getAttribute("FirstName");

getParameter("FirstName") - this method return only string object or null value. Its required do casting as per parameter value.

  Was this answer useful?  Yes

madhu81

  • Sep 22nd, 2006
 

putting it short , here it is ......

Data for getAttribue is from server side

Datat for getParameter is from client side

  Was this answer useful?  Yes

vamshidhar

  • Jan 15th, 2007
 

getParameter() method returns String and -it is used to get the form parameters.

getAttribute() method always returns Object-it is used to store and retrieve the objects from session

  Was this answer useful?  Yes

arunkumar

  • Sep 5th, 2007
 

getParameter() is used to get form field values. it will take form field name as argument, it is totally client side and returns String.

getAttribute() is used to get object value, which is set by setAttribute(),this method will take object key as aarguments, it is totally server side process and returns Object class Object, we need to convert to String

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions