JSP 2.0 & Beans
Hi All,
I just learnt that with JSP 2.0, we do not have to specify the useBean or getProperty tags. We can simple get the job done through........
${key.property}
... but this has really started me thinking about the necessity of having getters and setters within beans. What's the point... Because I was under the impression that even validation should not take place within beans. It should be taken care of within a seperate validation file. So provided that the getters and setters just simply return the variable; why bother having them in the first place ?
I am sure this is simply a misunderstanding on my part, therefore I would appreciate an explanation. Thanks.
[756 byte] By [
ibn_aziza] at [2007-10-3 2:32:47]

Getters and Setters provide the ability to encapsulate information.
Think of it this way, you want to provide access to the id of a record, but because the id is not supposed to change, you don't want people setting the id on your bean. So you would encapsulate it by providing a public getter, but with no (or at least not public) setter.
Hope that makes more sense.
Thanx for the replies,.
>So you would encapsulate it by
> providing a public getter, but with no (or at least
> not public) setter.
Let's hang on a sec. I thought one of the design constraints ( MVC ) to follow when implementing beans is that they must provide private data with public getters AND setters. So will it not be against the rules to ignore the setter for any variable ?
They refer to it as the MVC pattern, for a good reason. It's a pattern not a constraint.
Also MVC defines 3 layers (Model, View, Controller) of which we are only dealing with one (the Model layer).
And remember I just gave you an example as to why encapsulation is useful, if all you ever did was standard getting and setting (ie. this.firstName = firstName, return firstName), you're quite right, getters and setters would be quite useless.
But the whole idea behind getter/setters is encapsulation, having the ability to constrain (either explicitly, or allowing for future modification) access to information.
Thank you very much, but with your permission, I'd just like to clarify that I am aware that getters and setters are NORMALLY used for encapsulation, ie desktop applications. But this is not what I am woried about. My problem is with beans. I'd just like to know whether the MVC "pattern" allows this behaviour within them. The only problem I am facing is that I was under the impression that this was not allowed for beans.
So could you simply re-confirm that beans do not demand both getters and setters to be present for every data variable and that it is possible to have some sort of processing ( which I thought was only allowed with servlets ) such as validation within beans.
Much appreciated.