my dog learned polymorphism*
The moose likes Servlets and the fly likes Parameters and Attributes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Parameters and Attributes" Watch "Parameters and Attributes" New topic
Author

Parameters and Attributes

velmurugan
Greenhorn

Joined: Jun 08, 2006
Posts: 24
Hi everybody,

What is the difference between Attributes and Parameters.
that is difference between two methods request.getAttribute() and request.getParameter() and why we have this two methods?

Please help me
Sol Mayer-Orn
Ranch Hand

Joined: Nov 13, 2002
Posts: 311
1. Parameters are strings sent along with the http request. They arrive from the client browser.
E.g:
If a user prints the following in his browser's address:
http://yourHost/yourWebapp/yourServlet?favoriteColor=red
Then your servlet gets a parameter (named 'favoriteColor'):
// your Servlet.service() method:
String c=request.getParameter("favoriteColor"); // "red"

2. Attributes are server-side objects, created by the servelt programmer.
Each HttpRequest object has a map-like storage area, where the servlet programmers can store any objects they like (mapped by names or keys).
So, somewhere in your servlet code, a programmer can decide to store some value (note this comes from the programmer, and has nothing to do with the browser):
// your Servlet.service() method:
request.setAttribute("favoriteNumber", new Integet(7));
...// and it can later be retrieved using:
Integer i=(Integer) request.getAttribute("favoriteNumber").

Note: request attributes make most sense where used along with the RequestDispatcher API. Namely, when a single http request is handled by a chain of servlets/JSPs (e.g. one servelt does some calculation, another servlet or JSP builds the page header, another builds the main content...).
In such cases, the first servlet can do some calculation and store as a request attribute, so that it's available for the next servlets in the chain.
Attributes are *not* very useful if your request is handled by one simple Servlet (and you don't split the work between other servlets / JSPs).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parameters and Attributes