0% found this document useful (0 votes)
3 views16 pages

Chapter 7 Java Beans

JavaBeans is a portable, reusable software component model in Java that allows for visual manipulation in builder tools, featuring properties, events, and introspection. The J2EE APIs facilitate systems integration and enterprise application development through various components like enterprise beans and JSP pages. JavaBeans can encapsulate multiple objects, making them accessible from different locations, and are defined by specific properties and methods for customization.

Uploaded by

amanuelgurara55
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Chapter 7 Java Beans

JavaBeans is a portable, reusable software component model in Java that allows for visual manipulation in builder tools, featuring properties, events, and introspection. The J2EE APIs facilitate systems integration and enterprise application development through various components like enterprise beans and JSP pages. JavaBeans can encapsulate multiple objects, making them accessible from different locations, and are defined by specific properties and methods for customization.

Uploaded by

amanuelgurara55
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter 7

Java Beans
What is JavaBean
▪ JavaBeans is a portable, platform-independent component model
written in the Java programming language, developed in
collaboration with industry leaders.
▪ A Java Bean is a reusable software component that can be
manipulated visually in a builder tool.
▪ Simple / Complex
▪ Visible or Invisible
▪ The JavaBeans architecture brings the component development
model to Java
▪ Components are self-contained elements of software that can be
controlled dynamically and assembled to form applications.
▪ Software component (following a set of rules and guidelines; properties,
events, persistence)
▪ Builder tool
▪ Visual manipulation
2
Bean Features
▪ Typical unifying features that distinguish a Bean are:-
▪ Properties:- Enable developers to customize and program
with Beans
▪ Persistence:- Enables developers to customize Beans in an
app builder, and then retrieve those Beans, with customized
features intact, for future use
▪ Events:- Enables Beans to communicate and connect
together
▪ Introspection:- Enables a builder tool to analyze how a
Bean works
▪ Customization:- Enables a developer to use an app builder
tool to customize the appearance and behavior of a Bean
3
Systems and Application Integration
▪ The J2EE APIs enable systems and applications
integration through the following:
▪ Unified application model across tiers with enterprise beans
▪ Simplified response and request mechanism with JSP pages
and servlets
▪ Reliable security model with JAAS
▪ XML-based data interchange integration with JAXP
▪ Simplified interoperability with the J2EE Connector
Architecture
▪ Easy database connectivity with the JDBC API
▪ Enterprise application integration with message-driven
beans and JMS, JTA, and JNDI
4
J2EE Technology Architecture
html

Application clients Web clients

IIOP,
others
Java Server
Servlets
pages

Enterprise Java Beans Components

Server
JTS JMAPI JNDI JMS JAXP JDBC JAAS …
platform

5
Enterprise Java Bean(EJB)

▪ An enterprise bean is a server-side component that


contains the business logic of an application. At
runtime, the application clients execute the business
logic by invoking the enterprise bean's methods.
▪ Main goal of Enterprise Java Bean (EJB) architecture
is to free the application developer from having to
deal with the system level aspects of an application.
This allows the bean developer to focus solely on the
logic of the application.

6
JSP - JavaBeans
▪ A JavaBean is a specially constructed Java class written in
the Java and coded according to the JavaBeans API
specifications.
o It provides a default, no-arg constructor.
o It should be Serializable and that which can implement
the Serializable interface.
o It may have a number of properties which can be read or written.
o It should provide methods to set and get the values of the
properties, known as getter and setter methods.
▪ Why use JavaBean?
▪ It is a reusable software component.
▪ A bean encapsulates many objects into one object so that we can
access this object from multiple places.
7
Simple Example of JavaBean
▪ package BeanPackage; ▪ public String getLastName(){ return lastName;
▪ public class PersonInfo implements ▪ }
[Link] {
▪ public int getAge(){ return age;
▪ private String firstName = null;
▪ }
▪ private String lastName = null;
▪ public void setFirstName(String firstName){
▪ private int age = 0;
▪ [Link] = firstName; }
▪ public PersonInfo() {

▪ }
▪ public void setLastName(String lastName){

▪ public String getFirstName(){ return firstName;


▪ [Link] = lastName; }

▪ } ▪ public void setAge(Integer age){

▪ public String getLastName(){ return lastName; ▪ [Link] = age;

▪ } ▪ }}

▪ public int getAge() { return age; } 8


JavaBeans Properties
▪ A JavaBean property is a named feature that can be accessed by the
user of the object.
o The feature can be of any Java data type, containing the classes that you define.

▪ A JavaBean property may be read, write, read-only, or write-only.


o JavaBean features are accessed through two methods in the JavaBean's
implementation class:
1. getPropertyName () - Accessor.
▪ For example, if the property name is firstName, the method name would be
getFirstName() to read that property.

2. setPropertyName () - Mutator
▪ For example, if the property name is firstName, the method name would be
setFirstName() to write that property.
9
jsp:useBean action tag
▪ The jsp:useBean action tag is used to locate or instantiate a
bean class.
▪ If bean object of the Bean class is already created, it doesn't
create the bean depending on the scope. But if object of bean
is not created, it instantiates the bean.
▪ Syntax of jsp:useBean action tag :-

<jsp:useBean id= "instanceName" scope= "page | request | session | app


lication" class= "[Link]" type= "[Link]
sName" beanName="[Link] | <%= expression >" >
</jsp:useBean>
10
Attributes and Usage of jsp:useBean action tag (1)
1. id: is used to identify the bean in the specified scope.
2. scope: represents the scope of the bean. It may be page,
request, session or application.
1. page: specifies that you can use this bean within the JSP page.
It the default scope.
2. request: specifies that you can use this bean from any JSP
page that processes the same request. It has wider scope than
page.
3. session: specifies that you can use this bean from any JSP page
in the same session whether processes the same request or
not. It has wider scope than request.
4. application: specifies that you can use this bean from any JSP
page in the same application. It has wider scope than session.
11
Attributes and Usage of jsp:useBean action tag (2)
3. class:- instantiates the specified bean class (i.e. creates an
object of the bean class) but it must have no-arg or no
constructor and must not be abstract.
4. type:- provides the bean a data type if the bean already exists
in the scope.
o It is mainly used with class or beanName attribute.
o If you use it without class or beanName, no bean is instantiated.
5. beanName:- instantiates the bean using the
[Link]() method.

12
Simple example of jsp:useBean action tag
▪ //Java Class ▪ //[Link]
▪ package JBeans; ▪ <%@page contentType="text/html"
pageEncoding="UTF-8"%>
▪ public class Calculator {
▪ <jsp:useBean id="obj"
▪ public int cube(int n) {
class="[Link]"/>
▪ return n*n*n;
▪ <%
▪}
▪ int m=[Link](5);
▪ public int product(int a, int b)
▪ [Link]("cube of 5 is "+m);
▪{
▪ int pr=[Link](5,6);
▪ return a*b;
▪ [Link]("Product of 5 and 6 is "+pr);
▪}}
▪ %> 13
get and set properties Example
▪ package BeanPackage; ▪ String getProductBrand() {
▪ import [Link]; ▪ return productBrand;

▪ public class ProductsBean implements Serializable{ ▪ }

▪ private String productName = null; ▪ public void setProductBrand(String


productBrand) {
▪ private String productBrand = null; ▪ [Link] = productBrand;

▪ private int price = 0; ▪ }

▪ public ProductsBean(){ ▪ public int getPrice() {


▪ return price;
▪ }
▪ }
▪ public String getProductName() {
▪ public void setPrice(int price) {
▪ return productName; }
▪ [Link] = price;
▪ public void setProductName(String
productName) { ▪ }
▪ } 14
▪ [Link] = productName; }
[Link]
▪ <%@page contentType="text/html" pageEncoding="UTF-8"%>

▪ <html> <head>

▪ <title>get and set properties Example</title>

▪ </head>

▪ <body>

▪ <jsp:useBean id = "Products" class = "[Link]">

▪ <jsp:setProperty name = "Products" property = "productName" value = "Flash disk"/>

▪ <jsp:setProperty name = "Products" property = "productBrand" value = "SanDisk"/>

▪ <jsp:setProperty name = "Products" property = "price" value = "300"/>

▪ </jsp:useBean>

▪ <p>Product Name: <jsp:getProperty name = "Products" property = "productName"/> </p>

▪ <p>Product Brand: <jsp:getProperty name = "Products" property = "productBrand"/> </p>

▪ <p>Product Price: <jsp:getProperty name = "Products" property = "price"/> </p>

▪ </body></html>
15
END

16

You might also like