• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parameter vs. Attribute

 
Darryl Failla
Ranch Hand
Posts: 129
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get confused at times over the difference between a parameter and an attribute in servlet programming. Every time I think I have it clear, I come across something that re-clouds the issue. Can I get a hard, clear clarification from someone?
 
Marilyn de Queiroz
Sheriff
Posts: 9065
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you mean "attribute" and not "argument"?

Attribute = field
private int i = 0 ;
'i' is an attribute of the class that contains it because it is a member variable.

Parameter is what the method receives.

public static void main( String[] args )
In the above a String array named "args" is the parameter to the main method.
 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm talking about the getAttribute(), setAttribute(), and getParameter() methods of the ServletRequest interface. It seems that you can pass information via servlet as an attrubute or as a parameter. Why use one over the other?
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1820
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In HTML, fields that further qualify an element are called attributes. So for example:
< input type = text name = text size = 50 >
"Type", "name", and "size" are attributes of the HTML element "input" (often (not entirely correctly) called a tag).
When you go to process this same data (say, in Java servlet code) it gets called "parameters" because now on this side of things it's viewed as something to pass into a method. (Note too that if you're using the raw Sun API, you have to work with these in terms of "parameter names" and "parameter values.")
 
Darryl Failla
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this the general idea? If you want to pass a message to a servlet, you pass it as a hidden form field, and read it with getParameter(). If you want to pass an object (reference) to a servlet, you use setAttribute() and getAttribute().
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Darryl Failla:
Is this the general idea? If you want to pass a message to a servlet, you pass it as a hidden form field, and read it with getParameter(). If you want to pass an object (reference) to a servlet, you use setAttribute() and getAttribute().

If you want to get values from HTML controls like textareas etc, you use request parameters. If you want to initialize your servlets and/or servlet context, you use initial parameters. If you want to pass data around in your request, session or servlet context objects, you use attributes.
 
Louise Haydu
Ranch Hand
Posts: 51
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, parameters are data items that are sent in the request from the client side, with the client side being an HTML page. You can get these parameters, but you can't set them.
Attributes can also appear in the request (and a couple of other scopes as well). You can both get and set attributes.
Once a servlet gets a request, it can add additional attributes, then forward the request off to another servlet for processing. Attributes allow servlets to communicate with one another.
Hope this helps!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic