File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Difference between a Parameter and an Attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Difference between a Parameter and an Attribute" Watch "Difference between a Parameter and an Attribute" New topic
Author

Difference between a Parameter and an Attribute

Mocha D'Late
Greenhorn

Joined: Oct 27, 2002
Posts: 11
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

Joined: Mar 18, 2003
Posts: 118
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

Joined: Sep 23, 2000
Posts: 3253
    
    2
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.


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
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

Joined: Sep 23, 2000
Posts: 3253
    
    2
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.
Ken Januski
Ranch Hand

Joined: Aug 08, 2002
Posts: 130
Thanks for that explanation Paul.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between a Parameter and an Attribute
 
Similar Threads
abstract class Vs interface
query related to sessions
ServletContext
Java Question
Listeners and Distributed JVMs