Java Beans

1)What is a JavaBean?

JavaBeans are reusable software components written in the Java programming language, designed to be manipulated visually by a software develpoment environment, like JBuilder or VisualAge for Java. They are similar to Microsoft's ActiveX components, but designed to be platform-neutral, running anywhere there is a Java Virtual Machine (JVM).

2)What is a serialized bean?

You can deliver a bean as either its bytecode (compiled Java code) or as a serialized object (after the bean instance is serialized to a file (*.ser)).

The difference is that when you load the bean by its bytecode the bean loader normally create an instance from it by calling the default constructor (no arguments constructor). If you have a serialized instance the loader creates the instance like it was when it was serialized and therefore its internal state is recreated (except transient fields).

Therefore you can provide already customized beans for a particular purpose instead of letting the bean user do this. So you can provide beans for the different Swing look and feels which vary in its background color, borders, fonts, etc. Then the bean user can select for the one he prefers instead of trying settings the bean properties.

3)What is a servlet bean?

A servlet bean is a serializable servlet that follows the JavaBeans component architecture, basically offering getter/setter methods.

As long as you subclass GenericServlet/HttpServlet, you are automatically Serializable.

If your web server supports them, when you install the servlet in the web server, you can configure it through a property sheet-like interface.

4) Is a Java Bean a server side component or client side component?

A JavaBean component can be used anywhere, either like an AWT component on the client or with a JSP page on the server. There is nothing that requires it to be one or the other. In fact, the same bean can be used for both.

5) How can I access EJBs from JavaBeans Components?

In short, JavaBeans are not special; you access EJBs from JavaBeans the way you would from any other EJB client.

However, JavaBeans can be useful as a 'wrapper' for EJBs that hide some of the complexities of EJBs and allow their use from other Java programs. A visual JavaBean, in particular, can be assembled in a GUI "bean builder" application, while an EJB often cannot. However, adapting an EJB to become a JavaBean can be very complicated and should not be undertaken lightly.

6) With regards to JavaBeans, what is a manifest file and how is it used?

You can think of the manifest file as the Jar-file meta-data, describing the contents of the files inside the Jar. Each file in the .jar file can have an entry in the manifest file. This entry can include information stating that the entry is a JavaBean component and/or whether or not the entry has been signed.

For JavaBeans, the manifest file will be read when you import the JAR into your Bean builder tool. The tool will search the manifest file for those entries that have been flagged as JavaBeans, and more likely then not, add them to the tools component palette.

7) How can I read the initialization parameters for a bean, i.e. similar to what happens in servlets when we user the ServletConfig class and then use it's getInitParameter() function?

The initial version of the JavaBeans API included no direct support for this. You would have had to program it in yourself. The simplest way to do this was to use a (java.util.)Properties file and load() / store() key-value pairs in it.

Use either the getResourceAsStream() or getSystemResourceAsStream() method of the ClassLoader to locate the file.

8) What is introspection in JavaBeans?

Introspection is the ability to ask a JavaBean component what properties and events it supports. By default, introspection is supported by reflection, where you name methods with certain naming patterns, like set/getProperty() and add/removeListener(). You can also explicitly expose the bean's behavior through creation of a BeanInfo class related to the bean. The BeanInfo class can be used for many things, like setting up an icon for the component when used in a bean builder tool like JBuilder, limiting the properties available, or mapping methods to properties that don't follow standard naming patterns.

9) Is it possible to create static properties/methods of a bean class? Does the beans spec cover this and can builder tools handle a singleton bean, for example?

There is nothing that stops you from creating static methods within a Bean class. However, they won't be treated as properties. Also not supported is creating beans from methods other than the constructor... though instances can be serialized to .ser files and instantiated without calling the default/no-arg constructor through Beans.instantiated().

10) How should I model parameter passing in JSP and the relationship between JSP and JavaBeans using UML?


There are two principal ways to model parameters being passed from a HTML source on the client to a <> on the server. The first is to put them in a tag value, called parameters. For example:

    +------------------+                              +--------------------+
    | <>  |         <>        0..n |  <>   |  
    |     Catalog      |----------------------------->|    Product.jsp     |
    +------------------+         {prameters="prodid"} +--------------------+
                                                    <> |
                                                    |  <>   |
                                                    |     Product.html   |

In the above example the link from Catalog to Product.jsp indicates that a standard anchor tag is used (i.e. ) and that appended to the url is the parameter prodid. At design time this parameter is not given any value, since it is likely to be runtime generated thing. Also note that in this catalog page there are potentially many links to product pages, all with potentially different prodid params.

Multiple parameters can be passed simply be adding them in the string just as you would append them to the url. For example if the parameters tag value was "action=add&prodid&qty". It would indicate that at design time the parameter action was always set to add, and the values for prodid and qty are either not specified, or are specified at runtime.

If the parameters being passed to the server are more complex, and where it might be useful to document heavily each and every one. Then you might want to model them as and association class on the <> association. This lets you not only specific all the parameters, but gives you an oppertuity to specify datatypes, and default values for each. This of course is overkill for most simple parameter usages, but is handy for those special occasions.

    +------------------+                              +--------------------+
    | <>  |          <>            |  <>   |  
    |     HomeMonitor  |----------------------------->|    HomeControl.jsp |
    +------------------+        \                     +--------------------+
                           |  <>   |
                           |    ControlInfo    |
                           | homeid : long     |
                           | deviceid : long   |
                           | action : enum     |
                           | value : string    |


Modeling bean usage in a JSP begins with a <> stereotyped directional association from a <> class to the bean.

    +------------------+                                         +--------------------+
    | <>  |       <>  mySched : Schedule  |                    |  
    |   Calendar.jsp   |---------------------------------------->|   DutyScheduleBean |
    +------------------+                        {scope=session}  +--------------------+

The role name corresponds to the jsp:usebean id attribute, the role specifier Schedule corresponds to the type attribute, and the tag value for scope is the use bean scope attribute. The beanname attribute is another valid tag value that can be specified at design time.

11) What is the easiest way to convert my java beans into xml?

Use java.beans.XMLEncoder.

XMLEncoder e = new XMLEncoder(
                    new BufferedOutputStream(
                       new FileOutputStream("Test.xml")));
e.writeObject(new JButton("Hello, world"));

No comments: