Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Parameters and Attributes

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 311
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Sol Mayer-Orn Thanks a lot !
reply
    Bookmark Topic Watch Topic
  • New Topic