Difference between a Parameter and an Attribute

Mocha D'Late

Greenhorn

Posts: 11

posted 13 years ago

Hi In Request object interface, there are methods to get parameters and attributes. But in the interface for session object, only attributes. Whats the difference between a Parameter and an Attributes Thanks

Jonas Isberg

Ranch Hand

Posts: 118

posted 13 years ago

A parameter is something you pass when making a call to something. An attribute on the other hand holds a state.

The parameter 42 is passed to the constructor which sets the attribute to the parameter value. Hope this helped, I am not to familiar with the interfaces in question. [ July 17, 2003: Message edited by: J Isberg ]

Paul Anilprem

Enthuware Software Support
Ranch Hand

Posts: 3819

10

posted 13 years ago

Good explanation, Isberg. Another way to look at it is that parameters are not set by you (by the servlet programmer at run time) while attributes are always set by you. Notice that in no interface will you find setParameter() method but all interfaces ( request, session, and context) have setAttribute(). Further, parameters are always strings, while attributes can be any object.

Another thing to remember is that with Request the parameters often come from fields in a form. I could be wrong but I can't think of any other place they come from, though I guess you could just add them as a query string to the end of a http address. But most often I think they come from form input. So if you have fields like <input type="text" name="name"> and <input type="text" name="password"> two parameters will be passed with the Request object (name and password). This is in keeping with what has been said previously but it may also make it more concrete. Your web page then can retrieve those parameters with via Request.getParameter("name"), etc. However you really can't set them. You do indirectly by setting up a page that creates the parameters but you don't actually set them yourself. Attributes on the other hand can be both set and gotten by you. I'm not sure how often they're added to Requests but in my experience I often add them to a Session because I often have reason to put something in a Session object for later use.

Paul Anilprem

Enthuware Software Support
Ranch Hand

Posts: 3819

10

posted 13 years ago

Originally posted by Ken Januski:

I'm not sure how often they're added to Requests but in my experience I often add them to a Session because I often have reason to put something in a Session object for later use.

Request attributes are commonly used in Model 2 architecture where a servlet creates and fills up some beans and puts them into request (as attributes). It then forwards the request to a JSP page, which retrieves the attributes and uses them to generate the display html.