getParameter()
--------------------------------------------
This is used for sending a parameter from client side to server side.
Example :
On client side : http://test.com/servlet?parameter=hi
On server side : request.getParameter('parameter');
The value returned by the request.getParameter is always "String". So, even if you are sending a numeric value in the parameter, on server side it will be treated as string only. So if you want a different data type in that case you have to typecast it.
For example if you want a Long value from the parameter which is set as :
http://test.com/servlet?parameter=1 then
(Long) request.getParameter('parameter');
this will return a Long value but do check for null value else it will throw an NullPointerException.
getAttribute()
--------------------------------------------
This is commonly used on server side.
You can setAttribute value like this:
setAttribute('nameOfAttribute',valueOfAttribute);
And retrieve it like this :
getAttribute('nameOfAttribute');
Attributes can hold not only string but any kind of Object.Values set from server can be accessed on jsp using expressions or JSTL's. It is advised to use JSTL (JavaServer pages Standard Tag Library) in jsp page since they look more cleaner and easy to maintain than expressions.
You might also like :
Difference between Linkedlist and Arraylist
--------------------------------------------
This is used for sending a parameter from client side to server side.
Example :
On client side : http://test.com/servlet?parameter=hi
On server side : request.getParameter('parameter');
The value returned by the request.getParameter is always "String". So, even if you are sending a numeric value in the parameter, on server side it will be treated as string only. So if you want a different data type in that case you have to typecast it.
For example if you want a Long value from the parameter which is set as :
http://test.com/servlet?parameter=1 then
(Long) request.getParameter('parameter');
this will return a Long value but do check for null value else it will throw an NullPointerException.
getAttribute()
--------------------------------------------
This is commonly used on server side.
You can setAttribute value like this:
setAttribute('nameOfAttribute',valueOfAttribute);
And retrieve it like this :
getAttribute('nameOfAttribute');
Attributes can hold not only string but any kind of Object.Values set from server can be accessed on jsp using expressions or JSTL's. It is advised to use JSTL (JavaServer pages Standard Tag Library) in jsp page since they look more cleaner and easy to maintain than expressions.
You might also like :
Difference between Linkedlist and Arraylist
Comments
Post a Comment
.