java-beans
java-beans
A Java Bean is a software component that has been designed to be reusable in variety of
different environments.
There is no restriction on the capability of a Bean. It may perform a simple function,
such as spelling check of a document, or a complex function such as forecasting the
performance of a stock portfolio.
A Bean may be visible to an end user (ex. A button)
A Bean may be invisible to a user (ex. Software to decode a stream of multimedia
information in a real time)
A Bean may be designed to work autonomously on a user’s workstation or to work in
cooperation with a set of other distributed components.
A Bean may register to receive events from other objects and can generate events that are sent to
other objects
File: BeanDemo.Java
import java.io.Serializable;
public class BeanDemo implements Serializable
{
private String sname;
private String usn;
Getter Method
public void setsname(String sname)
{
For every Private Data
this.sname=sname; member of a bean class in
} order to access it, it is
public void setusn(String usn) compulsory to keep a getter
{ and setter method
this.usn=usn; Setter Method
}
public String getsname()
{
return sname;
}
public String getusn()
{
return usn;
}
}
File: BeanJSP-1.JSP
<html>
<body>
<jsp:useBean id="stu" scope="request“ class="beans.BeanDemo"/>
<jsp:setProperty name="stu" property="*"/>
<jsp:forward page="prog9b.jsp" />
</body>
</html>
File: BeanJSP-2.JSP
<html>
<body>
<jsp:useBean id="stu" scope="request" class="beans.Prog9" />
Name: <jsp:getProperty name="stu" property="sname"/></br>
USN: <jsp:getProperty name="stu" property="usn"/></br>
Branch: <%out.print(stu.getBranch());%></br>
</body>
</html>
<html>
<body><center><br><br>
<form action="BeanJsp-1.jsp" method="post">
Enter Name: <input type="text" name="sname"><br>
Enter usnno.: <input type="text" name="usn"><br>
Enter Branch: <input type="text" name="branch"><br><br>
<input type="submit" value="submit">
</center>
</form></body></html</html>
1) FormData.html this file is just used to pass the data to JSP File
2) BeanDemo Original Bean File which takes the form data as Private variables and
contains getter and setter methods for it.
3) BeanJsp-1.jsp this file takes the data from HTML file and stores the contents in Bena
File, this part of storing data is done by using the line
4) BeanJsp-2.jsp this file is called from BeanJsp-1.jsp by JSP action tag forward, this file
is used to display the contents stored in BeanDemo.java class
The Java Beans functionality is provided by a set of classes and interfaces in the
java.beans package.
Class Introspector
The Introspector class provides a standard way for tools to learn about the properties,
events, and methods supported by a target Java Bean.
For each of those three kinds of information, the Introspector will separately analyze the
bean's class and superclasses looking for information and use that information to build a
BeanInfo object that describes the target bean.
For each class "Foo", explicit information may be available if there exists a corresponding
"FooBeanInfo" class that provides a non-null value when queried for the information.
If a class provides explicit BeanInfo about itself then we add that to the BeanInfo
information we obtained from analyzing any derived classes.
We then proceed to analyze the class's superclass and add in the information from it.
What is Introspection?
Introspection is the automatic process of analyzing a bean's design patterns to reveal the
bean's properties, events, and methods. This process controls the publishing and
discovery of bean operations and properties
decapitalize(String name) : Utility method to take a string and convert it to normal
Java variable name.
flushCaches() : Flush all of the Introspector's internal caches.
flushFromCaches(Class classname) : Flush the Introspector's internal cached
information for a given class.
getBeanInfo(Class beanClass) : Introspect on a Java Bean and learn about all its
properties, exposed methods, and events.
getBeanInfo(Class beanClass, Class stopClass) : Introspect on a Java bean and learn all
about its properties, exposed methods, below a given "stop" point.
getBeanInfo(Class beanClass, int flags) : Introspect on a Java bean and learn about all
its properties, exposed methods, and events, subject to some control flags.
getBeanInfoSearchPath() : Gets the list of package names that will be used for finding
BeanInfo classes.
setBeanInfoSearchPath(String[] path) : Change the list of package names that will be
used for finding BeanInfo classes.
Introspection Sample
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
This example creates a non-visual bean and displays the following properties derived
from the BeanInfo object:
o class
o name
o size
Note that a class property was not defined in the SimpleBean class.
This property was inherited from the Object class. To get properties defined only in the
SimpleBean class, use the following form of the getBeanInfo method:
Introspector.getBeanInfo( SimpleBean.class, Object.class );
java.lang.Object
java.beans.FeatureDescriptor
java.beans.PropertyDescriptor
PropertyDescriptor(String, Class)
o Constructs a PropertyDescriptor for a property that follows the standard Java
convention by having getFoo and setFoo accessor methods.
PropertyDescriptor(String, Class, String, String)
o This constructor takes the name of a simple property, and method names for
reading and writing the property.
PropertyDescriptor(String, Method, Method)
o This constructor takes the name of a simple property, and Method objects for
reading and writing the property.
createPropertyEditor(Object bean)
o Constructs an instance of a property editor using the current property editor
class.
equals(Object obj)
o Compares this PropertyDescriptor against the specified object.
getPropertyEditorClass()
o Gets any explicit PropertyEditor Class that has been registered for this property.
getPropertyType() Gets the Class object for the property.
getReadMethod() Gets the method that should be used to read the property value.
getWriteMethod() Gets the method that should be used to write the property value.
hashCode() Returns a hash code value for the object.
isBound() Updates to "bound" properties will cause a "PropertyChange" event to get
fired when the property is changed.
isConstrained() Attempted updates to "Constrained" properties will cause a event to
get fired when the property is changed.
setBound(boolean bound)
o Updates to "bound" properties will cause a "PropertyChange" event to get fired
when the property is changed.
setConstrained(boolean constrained)
o Returns the class for the desired PropertyEditor. Normally, property editors will
be found using the property editor manager.
setPropertyEditorClass(Class propertyEditorClass)
o Normally PropertyEditors will be found using the PropertyEditorManager.
setReadMethod(Method readMethod)
o Sets the method that should be used to read the property value.
setWriteMethod(Method writeMethod)
o Sets the method that should be used to write the property value.
java.lang.Object
java.beans.FeatureDescriptor
java.beans.EventSetDescriptor
Constructor Details:
Creates an EventSetDescriptor assuming that you are following the most simple
standard design pattern where a named event "fred" is (1) delivered as a call on the
single method of interface FredListener, (2) has a single argument of type FredEvent, and
(3) where the FredListener may be registered with a call on an addFredListener method
of the source component and removed with a call on a removeFredListener method.
Parameters:
sourceClass - The class firing the event.
eventSetName - The programmatic name of the event. E.g. "fred". Note that this should
normally start with a lower-case character.
listenerType - The target interface that events will get delivered to.
listenerMethodName - The method that will get called when the event gets delivered to its
target listener interface.
Throws:
IntrospectionException - if an exception occurs during introspection.
Method Detail
getListenerType
getListenerMethods
getListenerMethodDescriptors:
getAddListenerMethod
getRemoveListenerMethod
java.lang.Object
java.beans.FeatureDescriptor
java.beans.MethodDescriptor
Constructor Summary
MethodDescriptor(Method method)
Constructs a MethodDescriptor from a Method.
MethodDescriptor(Method method, ParameterDescriptor[] parameterDescriptors)
Constructs a MethodDescriptor from a Method providing descriptive information for each of
the method's parameters.
Method Summary
Method getMethod()
Gets the method that this MethodDescriptor encapsualtes.
ParameterDescriptor[] getParameterDescriptors()
Gets the ParameterDescriptor for each of this MethodDescriptor's
method's parameters.