Published on


Published in: Education, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. WEB PROGRAMMING UNIT-IV PREPARED BY BHAVSINGH MALOTH JAVA BEANS: o Java bean is a reusable s software component which is visible and customizable.  A JavaBean is a specially constructed Java class written in the Java and coded according to the JavaBeans API specifications. Following are the unique characteristics that distinguish a JavaBean from other Java classes:  It provides a default, no-argument constructor.  It should be serializable and implement the Serializable interface.  It may have a number of properties which can be read or written.  It may have a number of "getter" and "setter" methods for the properties. JavaBeans Properties:   A JavaBean property is a named attribute that can be accessed by the user of the object. The attribute can be of any Java data type, including classes that you define. A JavaBean property may be read, write, read only, or write only. JavaBean properties are accessed through two methods in the JavaBean's implementation class: Method getPropertyName() setPropertyName()  Description For example, if property name is firstName, your method name would be getFirstName() to read that property. This method is called accessor. For example, if property name is firstName, your method name would be setFirstName() to write that property. This method is called mutator. A read-only attribute will have only a getPropertyName() method, and a writeonly attribute will have only a setPropertyName() method. Page  The properties, events, and methods of a bean that are exposed to an application builder tool can be controlled. 1 ADVANTAGES OF JAVA BEANS:  A bean obtains all the benefits of java’s “write once, run anywhere” paradigm.
  2. 2.  A bean may be designed to operate correctly in different locals, which makes it useful in global markets.  The configuration settings of a bean can be saved in persistent storage and restored at a later time.  A bean may register to receive events for other objects and can generate events that are sent to other objects. Disadvantages with JavaBeans  A class with a nullary constructor is subject to being instantiated in an invalid state. If such a class is instantiated manually by a developer (rather than automatically by some kind of framework), the developer might not realize that the class has been improperly instantiated. The compiler can’t detect such a problem, and even if it’s documented, there’s no guarantee that the developer will see the documentation.  Having to create a getter for every property and a setter for many, most, or all of them can lead to an immense quantity of boilerplate code.  WHY USE JAVA BEAN? Page 2 According to Java white paper, it is a reusable software component. A bean encapsulates many objects into one object, so we can access this object from multiple places. Moreover, it provides the easy maintenance.
  3. 3. Page Starting the BDK: To start the BDK, follow these steps:  Change to the directory c:bdkbeanbox.  Execute the batch file called “run.bat” This causes the BDK to display the three windows. Toolbox lists all of the different beans that have included with the BDK. Bean box provides an area to layout and connect the beans selected from 3 The Bean Developer Kit (BDK):  The bean Developer kit (BDK), available for the java soft site is a simple example of a tool enables you to create, configure and connect a set of beans.
  4. 4. the toolbox. Properties provide the ability to configure a selected bean. JAR FILES:  JAR files-stands for Java Archive Files.  A jar file allows you to efficiently deploy a set of classes and their associated resources. A set of beans can be placed into one JAR file. Page 4 1
  5. 5. Jar technology makes it much easier to deliver and install software. Also, the elements in a Jar file are compressed, which makes down loading a JAR file much faster than separately downloading several uncompressed files. MANIFEST FILES: A developer must provide a manifest file to indicate, which of the components in a JAR file are Java Beans. An example of a manifest file is provided in the following listing. It defines a JAR file that contains four “.gif” files and one “.class” file. The last entry is a bean. Ex: NAME: sunw/deco/slides/slide0.gif NAME: sunw/deco/slides/slide1.gif NAME: sunw/deco/slides/slide2.gif NAME: sunw/deco/slides/slide3.gif NAME: sunw/deco/slides/slides.class Java-Bean: True Creating a JAR file: The following command creates a Jar file named “xyz.jar” that contains all of the “.class” and “.gif” files in the current directory Ex: Jar cf xyz.jar *.class *.gif If a manifest file such as xyz.mf is available, it can be used with the following command: Ex: Jar cfm xyz.jar yxz.mf *.class *.gif INTROSPECTION: A quality of a bean to express the properties of itself is known as Introspection. Introspection can be done in two ways: i. Simple naming conventions ii. Providing additional classes to have introspection Simple naming convention makes use of setter and getter methods. In case of providing additional classes, for a bean class, to have the properties of the bean, another class files are written, called for ex: Bean property class. 1.Bean Naming Conventions: • “setters” – set or modify a bean property value – “getters” retrieve property values from a bean – must start with “get” 5 – Page • must start with “set”
  6. 6. For ex: Simple naming convention bean will be like Class bean1 { : : Property1 ( ) { } Property2 ( ) { } : : } For ex: providing additional classes will be like: Class bean1 { : : } Class properties { : : } Page 6 Design patterns for Properties: A property is a subset of a beans state. Three types of properties: Simple Boolean Indexed
  7. 7. Simple Properties: A simple property has a single value. I t can be identified by the following design patterns, where N is the name of the property and T is the type. Ex: public T getN( ); public void setN( T arg); Boolean Properties: A Boolean property has a value of true or false. It can be identified by the following design patterns, where N is the name of the property. Ex: public Boolean isN( ); public Boolean getN( ); public void setN(Boolean value); Indexed Properties: An Indexed property consists of multiple values. It can be identified by the following design patterns, where N is the name of the property and T is the type: Ex: public T getN (int index); public void setN(int index, T value); public void setN(T values[]); Creating a new Bean: The steps that are involved in creating a new bean are: 1. Create a directory for the new bean. 2. Create the java source file(s). 3. Compile the source file(s). 4. Create a manifest file. 5. Generate a JAR file. 6. Start the BDK. 7. Test. Creating a Bean: Step 1: import java.awt.*; import java.io.*; public class Bean1 extends Canvas implements Serializable { public Bean1 ( ) { setSize(60,40); setBackground(Color.red); } } Step 2: javac Bean1.java Step 3: Manifest.txt Name: Bean1.class Java-Bean:true Step 4: jar cfm demo.jar manifest.txt Bean1.class 3 Dept. of Computer Science Step 5: Open the bean box: Page 7 Java Bean
  8. 8. i. In the folder c:bdkbeanboxrun.bat ii. “run.bat” will open the bean box tool. Step 6: File->Load->demo. jar / (or) Copy demo.jar in beans / jars directory. EVENTS A bean class can fire off any type of event, including custom events. As with properties, events are identified by a specific pattern of method names. public void add<Event>Listener(<Event>Listener a) public void remove<Event>Listener(<Event>Listener a) The listener type must be a descendant of java.util.EventListener. For example, a Swing JButton is a bean that fires action events when the user clicks on it. JButton includes the following methods (actually inherited from AbstractButton), which are the bean pattern for an event: public void addActionListener(ActionListener l); public void removeActionListener(ActionListener l); Bean events are recognized by builder tools and can be used in wiring components together. For example, you can wire a button's action event to make something happen, like invoking another bean's method. Customizers    The JavaBeans specification provides for user-defined customizers, through which you can define a higher level of customization for bean properties than is a available with property editors. When you use a bean Customizer, you have complete control over how to configure or edit a bean. A Customizer is an application that specifically targets a bean’s customization. Sometimes properties are insufficient for representing a bean's configurable attributes. Customizers are used where sophisticated instructions would be needed to change a bean, and where property editors are too primitive to achieve bean customization. All customizers must: Implement the java.beans.Customizer interface This means implementing methods to register PropertyChangeListener objects, and firing property change events at those listeners when a change to the target bean has occurred. 8  Extend java.awt.Component or one of its subclasses. Page 
  9. 9.  Implement a default constructor. Associate the customizer with its target class via BeanInfo.getBeanDescriptor. JavaBeans API The JavaBeans functionality is provided by a set of classes and interfaces in the java.beans package. Interface Description Methods in this interface are used to initialize Beans that are AppletInitializer also applets. This interface allows the designer to specify information about BeanInfos the events, methods and properties of a Bean. This interface allows the designer to provide a graphical user Customizer interface through which a bean may be configured. Methods in this interface determine if a bean is executing in DesignMode design mode. A method in this interface is invoked when an exception has ExceptionListener occurred. A method in this interface is invoked when a bound property is PropertyChangeListener changed. Objects that implement this interface allow the designer to PropertyEditor change and display property values. A method in this interface is invoked when a Constrained VetoableChangeListener property is changed. Visibility Methods in this interface allow a bean to execute in environments where the GUI is not available. JAVABEAN AP I: Page 9  The JavaBeans API architecture supplies a set of classes and interfaces to provide introspection.  The following figure represents a hierarchy of the FeatureDescriptor classes:
  10. 10. INTRODUCTION EJB     Enterprise Java Beans (EJB) is development architecture for building highly scalable and robust enterprise level applications to be deployed on J2EE compliant Application Server such as JBOSS, Web Logic etc. EJB 3.0 is being a great shift from EJB 2.0 and makes development of EJB based applications quite easy. EJB stands for Enterprise Java Beans. EJB is an essential part of a J2EE platform. J2EE platform have component based architecture to provide multi-tiered, distributed and highly transactional features to enterprise level applications. EJB provides an architecture to develop and deploy component based enterprise applications considering robustness, high scalability and high performance. An EJB application can be deployed on any of the application server compliant with J2EE 1.3 standard specification. We'll be discussing EJB 3.0 in this tutorial.  An EJB is just a collection of Java classes and XML file, bundled into a single unit. The Java classes must follow certain rules and provide certain callback methods.  EJB is just a specification. It is not a product.  EJBs are reusable components.  EJB is a widely-adopted server-side component architecture for J2EE. BENEFITS (OR) ADVANTAGES OF EJB Page 10  EJB components are designed to encapsulate business logic, and to protect the application developer from having to worry about system level issues.
  11. 11.         Application Server/ EJB container provides most of the system level services like transaction handling, logging, load balancing, persistence mechanism, exception handling and so on. Developer has to focus only on business logic of the application. EJB container manages life cycle of ejb instances thus developer needs not to worry about when to create/delete ejb objects. EJB components are server-side components written entirely in the Java programming language EJB components contain business logic only - no System-level programming System-level services (i.e. "plumbing") such as transactions, security, Life-cycle, threading, persistence, etc. are automatically managed for the EJB component by the EJB server EJB architecture is inherently transactional, distributed, portable, multi-tier, scalable and secure EJB components are fully portable across any EJB server and any OS, work with any client. Components are declaratively customized There are four major parts to every bean: the home interface, the remote interface, the implementation class and the XML deployment descriptor 11  Simplified development of large scale enterprise level application. Page 
  12. 12. EJB ARCHITECTURE:  EJB VS JAVABEANS  The JavaBeans architecture is meant to provide a format for general-purpose components whereas the EJB architecture provides a format for encapsulation and management of business logic.  JavaBeans has tier of execution at Client and EJB has at Server (specifically business logic tier) 1. Session Beans Page VAR IETIES OF BEANS : 12  In JavaBeans the runtime execution environment provides services like Java libraries, Java application etc. The EJB runtime environment provides services of Persistence, declarative transactions and security, connection pooling and lifecycle services.
  13. 13.   Stateful session bean Stateless session bean 2. Entity Beans   With container-managed persistence With bean-managed persistence 3. Message-Driven Beans TYPES EJB are primarily of three types which are briefly described below: Type Description Session Bean Session bean stores data of a particular user for a single session. It can be stateful or stateless. It is less resource intensive as compared to entity beans. Session bean gets destroyed as soon as user session terminates. Entity Bean Entity beans represents persistent data storage. User data can be saved to database via entity beans and later on can be retrived from the database in the entity bean. Message Driven Bean Message driven beans are used in context of JMS (Java Messaging Service). Message Driven Beans can consumes JMS messages from external entities and act accordingly. STATELESS SESS ION BE AN LIFE CYCLE 1. There are two stages in the Lifecycle of Stateless Session Bean. These are: Page 3. b) Method Ready Pool In the Method Ready Pool stage bean has instance(s) in the memory of the EJB container and it is ready to serve clients. On the startup of the EJB container some instances of the bean are created and placed in the pool. EJB container creates the new instance of the Bean and then sets the session context (setSessioncontext()) and it calls the ejbCreate() method to place the bean in the Method Ready Pool 13 2. a) Does Not Exist In the Does Not Exist stage, bean does not have instance in the memory. In this stage bean has not been instantiated.
  14. 14. stage. Container calls the ejbRemove() method to move the bean into Does Not Exist state. 4. Following Diagram shows the Life cycle of Stateless Session Bean STATEFUL SESS ION BEAN LIFE CYCLE There are there stages in the life cycle of Stateful Session bean Life cycle. These are: a) Does Not Exist This is the Does Not Exist stage, bean does not have instance in the memory. In this stage bean has not been instantiated. b) Method Ready Pool In the Method Ready Pool stage bean has instance in the memory of the EJB container and it is ready to serve client. One instance of the Stateful Session Bean servers only one client. When Client Calls create(args) method on the Home Interface, server creates new Page c) Passive state In the Passive state the bean is passivated to conserve the resource. The passivate method is called before the instance enters the "passive" state. The instance should release any resources that it can re-acquire later in the ejbActivate() method. After the passivate method completes, the instance must be in a state that allows the container to use the Java Serialization protocol to externalize and store away the instance's state. ejbRemove or Timeout moves the bean into Does Not Exist stage. 14 instance of the bean and sets the Session Context and then container calls the ejbCreate(args) method on the bean and places the bean into Method Ready Pool stage. ejbRemove or Timeout moves the bean into Does Not Exist stage.
  15. 15. Following Diagram shows the Life cycle of Statelful Session Bean Advertisement Page  A requirement of any of the services provided by an EJB container like transactions, scalability, persistence, security, future growth possibilities is an appropriate reason to use EJB in the design of the application. 15 WHY USE EJBS IN YOUR DES IGN?