Java Beans
Java Beans
Application Development
Introduction to JavaBeans
Topics
JavaBeans
Introduction
• What are they?
• Why should you care?
System.out.println(s.getName());
}
}
Output: My Student
JavaBean with JSP
The bean implemented on the following page is
a very simple JavaBean using JSP.
It contains a single property, three methods,
and no events.
The single property is both readable and writable.
public PhraseBean() {
phrase = “Hello, World!”;
}
Or
useBean Tag (cont)
Scope
Page
Request
Sesion
Application
Bean automatically initialize and referenced
in the declared scope
The same id in the same scope is not allowed
getProperty Tag
The getProperty tag retrieves the property
specified by the property attribute from the
bean specified by the name attribute.
Before you can get a bean’s properties, that bean
must be created with the useBean tag.
The example in the code retrieves the current
value of the phrase bean’s phrase property:
<jsp:getProperty name="phrase" property="phrase"/>
getProperty Tag
Instead of using the getProperty tag:
<jsp:getProperty name="phrase" property="phrase"/>
public CountBean() {
count = 0;
countOk = true;
}
CountBean.java
public void setCount(String count) {
try {
this.count = Integer.parseInt(count);
this.countOk = true;
}
catch (Exception exc) {
this.countOk = false;
}
}
Event
Object
public PhraseBean() {
phrase = "Hello, World!";
phraseChangeListeners = new Vector();
}
Vector vec;
synchronized(this) {
vec = (Vector)phraseChangeListeners.clone();
}
bean.setPhrase("this.is.a.new.phrase");
}
}
Java Bean in MVC architecture
MVC is the most popular pattern architeture for
WEB application.
MVC – Model – View – Controller
Model: responsibility for processing business data
• Java Bean – DAO: Business data, Tier Data