Slide 7 JavaBean in JSP
Slide 7 JavaBean in JSP
03/04/25 2
Content
Component Object Model
Java Bean features
Introspection
Persistance
Java Bean class
Using Java Bean in JSP
Java bean with EL
Apply Java Bean in MVC application
03/04/25 3
Component Object Model
Component Object Model (COM) is an interface
standard for software components introduced by
Microsoft - 1983
The idea of COM is create objects that can be used in
environments different from the one in which they were created.
Component: independent software modules that can be
integrated into an application
Blocks, modules of a house
General COM technology
COM - Microsoft
Java bean from Sun
COM+, DCOM, EJB
Java Bean is a design pattern class follow COM concept
03/04/25 4
Java Bean features
Follow design patern
Introspection
The deployment environment has the ability to
automatically recognize the properties, methods, and
events of the Java Bean object
Naming design pattern
Bean Info class
Persistance
The ability to store and recover data changes of Java
Bean objects
Serializable
03/04/25 5
Java Bean class
Must have default, no-argument constructor.
implement the Serializable interface.
May have a number of properties which can
be read or written.
May have a number of "getter" and "setter"
methods for the properties.
03/04/25 6
Java Bean
Bean properties
Introspec by set/get method
public int getAccNo()
public void setAccNo(int id)
-> property named accNo with datatype int
Types of bean properties
simple properties
constraint properties
bound properties
index properties
03/04/25 7
Using Java Bean in JSP
Action useBean: runtime invocation JSP tag
Syntax
<jsp:useBean id=“varName” class=“BeanClassName”
scope=“effectiveScope” />
Note: if id of bean created ready use type not use class for the
second call this bean
<jsp:useBean id=“acc” class=“Account” scope=“session”/>
Scope: effective scope of bean– scope variable
Page
Request
Session
Application
Bean is automatically initialized & referenced in the declared scope:
the same id in same scope is not allowed!!!
03/04/25 8
03/04/25 9
Java Bean in JSP
Set the property of bean object
3 syntax:
<jsp:setProperty name=“var” property=“attbName” value=“a_value” />
Set a property (attbName) of bean (var) with a specfic value (a_value).
Set all bean's properties according to the input form's properties when the
name of the bean's property matches the name attribute on the input form
Map 1-1 from the input form to the bean's properties
Should be most widely used for convenience..
03/04/25 10
Java Bean in JSP
Get property of bean object
<jsp:getProperty name=“var” property=“attr” />
return current value of bean property attr
the same like getAttr()
Get bean property with EL syntax
${beanName.propertyName}
beanName is automatically lookup from narrow to
wide scope, similar to implicit object.
03/04/25 11
Java bean in MVC architechture
MVC is the most popular pattern for web
applications
MVC Model – View – Controller
Model: responsible for processing business data of
an application/system:
Java bean - DAO : business tier – data tier
View: responsible for data presentation and direct
user interaction
JSP – JSTL – EL – Custom Tags
Controller: responsible for controlling and
connecting components:
Servlet – Filter – Listener
03/04/25 12
Summary
Component model and MVC
Java Bean features
Using Java bean in JSP : action tags
Java beans are
Widely used in GUI programming environment
Encapsulated object for data exchange in a
distributed environment
03/04/25 13
Practical homework
Develop bean class Account, Customer,
TransLog for Banking application
Development of a Counter bean as a hit
counter for all pages of your application.
Where should counter bean be declared?
Using Java bean for user registration of web
application
Using Java bean for listing data of a web
application
03/04/25 14
Constructivity question
State the benefits of using java bean in java
web application
How to access a bean object declared on the
JSP page when coding the servlet?
How is access to different beans with the
same name done?
Try to find the reason why a java bean object
cannot be accessed on the JSP page even
though there is no syntax error.
03/04/25 15