A: The law according to the creakingly-ancient JavaBeans specification. We’re talking JavaBeans—NOT Enterprise JavaBeans (EJB) which is completely unrelated. (Go figure.)The plain old non-enterprise JavaBeans spec defines what it takes for a class to be a JavaBean. Although the spec actually gets pretty complex, the only things you need to know for using beans with JSP and servlets are these few rules (we’re showing only those that apply to what we’re doing with servlets and JSPs): 1) You MUST have a public, no-arg constructor. 2) You MUST name your public getter and setter methods starting with “get” (or “is”, for a boolean) and “set”, followed by the same word. (getFoo(), setFoo()). The property name is derived from stripping off the “get” and “set”, and changing the first character of what’s left to lowercase. 3) The setter argument type and the getter return type MUST be identical. This defines the property type. int getFoo() void setFoo(int foo) 4) The property name and type are derived from the getters and setters and NOT from a member in the class. For example, just because you have a private int foo variable does NOT mean a thing in terms of properties. You can name your variables whatever you like. The “foo” property name comes from the methods. In other words, you have a property simply because you have a getter and setter. How you implement them is up to you. 5) For use with JSPs, the property type SHOULD be a type that is either a String or a primitive. If it isn’t, it can still be a legal bean, but you won’t be able to rely only on standard actions, and you might have to use scripting. |