Difference between getAttribute() and getParameter()
SR
|
getAttribute()
|
getParameter()
|
1
|
The getAttribute() method always returns you an object
|
.getParameter() method always returns an string.
|
2
|
A "parameter" is a name/value pair sent from the client to the server - typically, from an HTML form. Parameters can only have String values. Sometimes (e.g. using a GET request) you will see these encoded directly into the URL (after the ?, each in the form name=value, and each pair separated by an &). Other times, they are included in the body of the request, when using methods such as POST.
|
using request.setAttribute(). You can add any type of object you like here, Strings, Custom objects, in fact any object. You add the attribute to the request and forward the request to another resource, the client does not know about this. So all the code handling this would typically be in JSP/servlets. You can use request.setAttribute() to add extra-information and forward/redirect the current request to another resource.
|
3
|
An "attribute" is a server-local storage mechanism - nothing stored in scoped attribues is ever transmitted outside the server unless you explicitly make that happen. Attributes have String names, but store Object values. Note that attributes are specific to Java (they store Java Objects),
|
while parameters are platform-independent (they are only formatted strings composed of generic bytes).
|
4
|
There are four scopes of attributes in total: "page" (for JSPs and tag files only), "request" (limited to the current client's request, destroyed after request is completed), "session" (stored in the client's session, invalidated after the session is terminated), "application" (exist for all components to access during the entire deployed lifetime of your application).
|
The bottom line is: use parameters when obtaining data from the client, use scoped attributes when storing objects on the server for use internally by your application only.
|
5
|
the servlet/jsp sets and reads attributes for communicating among themselves
|
Typically, parameters are send by the user browser via HTTP GET/POST,
|
No comments:
Post a Comment