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?
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
Joined: Oct 16, 2001
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?
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.")
Joined: Oct 16, 2001
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().
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.
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!