permaculture playing cards*
The moose likes Cattle Drive and the fly likes Parameter vs. Attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Parameter vs. Attribute" Watch "Parameter vs. Attribute" New topic
Author

Parameter vs. Attribute

Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
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?


Darryl Failla
Sun Certified Java 2 Programmer
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
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.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
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

Joined: Mar 25, 2001
Posts: 1752
    
    2
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

Joined: Oct 16, 2001
Posts: 129
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

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Aug 17, 2000
Posts: 51
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parameter vs. Attribute