hi friends, i just wonder what's difference between parameters and attributes. 1) is a parameter referring a string value, while an attribute a class? 2) we know there're three levels of attributes (parameters as well) coresponding to the three scope - request, session, and context. why do we define the attribute in three level? i guess it's for collaboration between servlets or applications, right? what's the sequence of the preference if there's a controdiction between attribute/parameter? thanks, tony
Well, first I would say that attributes are bound to the scope and can have listeners. whereas parameters don't have any listeners, and are passed in that call to the servlet. In the case of an init parameter, that is passed to the init() method of the Servlet, and can be used there in many ways. you could even then take those values and bind them as attributes into a the ServletContext. When you create attributes, they do not have to be in all three scopes at the same time. Actually if you wanted something like that, you would only have to bind it to the application or context scope, therefore any servlet or session will have access to that attribute via the context. Hope that helps to clear some of it up, and hope my answers are 100% correct. Mark
thanks mark so can we say parameters are more "static" while attributes are more "dynamic"? besides, could you give me a few examples where the implementation of attributes and their(events/listeners) are crutial to the application while parameters can't perform the tasks? tony
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
posted
0
The use of terms like "static" and "dynamic" is a little mis-leading in the J2EE paradigm. I would steer away from use of such terms to define what you are trying to define. Static predominantly refers to HTML and dynamic refers to content that is generated based on the request. Both parameters and attributes, in this sense are dynamic....one may argue. So, I will NOT classify them using those terms. Also, I din't get why you said there are "three levels of parameters as well". What does that mean? - satya
hi satya, i meant the three scopes in a web application. i find parameters are defind in web.xml while attributes can be set in runtime. that's why i referred them having static/dynamic characters. i'd make a list here: request scope declared in web.xml <servlet>..<para-name>..<para-value>.. session scope i can't figure out application scope defind in web.xml <context-para><para-name>..<para-value>.. for attributes , there's a setAttribute() in each of the three scope classes: ServletRequest, HttpSession, and ServletContext. sure, we can take an attribute of string as a parameter. in this case, such a parameter can be set in runtime too, right tony
Hey Satya, do you think my explanation between the difference between parameters and attributes is correct? Would you add anything to it, or any part that I am way off base on. One of the ways I also learn is by posting answers to questions, on ones that I think I have the answer, and if my answers is off a bit, then I get corrected and learn. Thanks Mark
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
posted
0
Hi,
1) is a parameter referring a string value, while an attribute a class?
A parameter is, and always, a String, while an attribute is an Object. There's a big difference there already.
we know there're three levels of attributes (parameters as well) coresponding to the three scope - request, session, and context.
Initial parameters can be specific to a servlet (but not session), or to a context. Parameters obtained from a control only have request scope.
what's the sequence of the preference if there's a controdiction between attribute/parameter?
I don't see how a contradiction may ensue. You retrieve a parameter by getParameter() while you retreive an attribute by getAttribute(), even if a parameter and an attribute have the same logical name. A more interesting question would probably be if we have two attributes with the same logical name but defined in different scopes.
tony lee
Ranch Hand
Joined: Jan 21, 2002
Posts: 52
posted
0
hi anthony, 1) don't you think there's a context parameter as far as i know, there's a <context-para> tag in web.xml. 2) what would happen if two attributes with the same name at different scopes? according to rules in java, i think the attribut in the smaller scope will overriden the one defined for more bigger scope, right? 3) i read a book referring sessions are scoped at application level. it seems to me one application has only one context. then, what's difference between context scope and session scope tony
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
posted
0
One of the ways I also learn is by posting answers to questions, on ones that I think I have the answer, and if my answers is off a bit, then I get corrected and learn That the best way, I believe, to learn. Thats one reason why I try to say out what I understand. Its very much possible that I understand it one way, but there could be a lot more meanings to the sentences in the Specs. So speak up and stay tuned.
Would you add anything to it, or any part that I am way off base on What you said is correct, but incomplete. You will agree with me, ofcourse, when I say there's more to it than what you have mentioned. But, what you said is one of the most important differences. Please speak up, say as much as you can, thats the best way to learn. My sincere suggestion......... - satya
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
posted
0
Tony: To be brutally honest: "You need to understand the terms better when you use them in J2EE context. If not, you will be sending the wrong signals." Please try and understand the differences and usage of the following terms which I picked up from this discussion: static dynamic scope parameter init-param context-param attribute application session I would respectfully disagree with you if you say there are three scopes of parameters. A parameter, IMO, is used to send a value during a request time, generally speaking. Better called request parameter. Initialization parameters are different and shouldn't be used in the same way as parameters, atleast I wouldn't want to. There are two types of initializations that you could perform - 1. one on the servlet context or context or application which are defined by "context-param" element of the DD. 2. second on the servlet. These are defined by the "init-param" element of the "servlet" element of the web-app. For what its worth........ - satya
Madhav Lakkapragada
Ranch Hand
Joined: Jun 03, 2000
Posts: 5040
posted
0
1) don't you think there's a context parameter as far as i know, there's a <context-para> tag in web.xml.
If you are talking of initialization parameters, yes you can have them and the "<context-param>" is the element you need to define these. I will agree with you if you say that the element name is a little misleading. But every element also comes with a discription, right? 2) what would happen if two attributes with the same name at different scopes? according to rules in java, i think the attribut in the smaller scope will overriden the one defined for more bigger scope, right? You might find this discussion helpful. Same name attributes discussion. 3) i read a book referring sessions are scoped at application level. it seems to me one application has only one context. then, what's difference between context scope and session scope
JSP Spec. p 70, Table JSP.4.1, session The named object is available from the current page’s HttpSession object (which can in turn be obtained from the ServletRequest object) using the getAttribute(name) method. This reference must be discarded upon invalidation of the current session. It is Illegal to change the value of an instance object so associated so that its new runtime type is a subset of the type(s) of the object previously so associated. Note it is a fatal translation error to attempt to use session scope when the JSP page so attempting has declared, via the <%@ page ... %> directive (see later) that it does not partic-ipate in a session. application The named object is available from the current page’s Serv-letContext object using the getAttribute(name) method. This reference shall be discarded upon reclamation of the ServletContext. It is Illegal to change the value of an instance object so associated, such that its new runtime type is a subset of the type(s) of the object previously so associated.
regds. - satya
Mee02 Re
Greenhorn
Joined: Feb 20, 2002
Posts: 8
posted
0
Is there any book for JSP Spec. u are saying it as pg 70
Anthony Villanueva
Ranch Hand
Joined: Mar 22, 2002
Posts: 1055
posted
0
Hi, THe JSP docs and specs are available on thislink. You might find this useful too. Cheers! -anthony