Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parameters and Attributes

 
velmurugan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 311
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Shiv Tattva
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Sol Mayer-Orn Thanks a lot !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic