SlideShare a Scribd company logo
Java Beans
Agenda
• Understanding the benefits of beans
• Creating beans
• Installing beans classes on server
• Accessing bean properties
What is JavaBean
• JavaBean is reusable software components
• Rules for creating Java Bean
- Public class
- Private/protected instance variable
- Zero-arg constructor
- Public getter and setter methods(accessor and mutator )
• Any java class which satisfies the above rule is a Java Bean
Why use a JavaBeans Component
• A JSP page can create and use any type of Java programming language object within a declaration or
scriptlet. The following scriptlet creates the bookstore shopping cart and stores it as a session attribute:
- <% ShoppingCart cart = (ShoppingCart)session. getAttribute("cart"); // If the user has no
cart, create a new one if (cart == null) { cart = new ShoppingCart();
session.setAttribute("cart", cart); } %>
• If the shopping cart object conforms to JavaBeans conventions, JSP pages can use JSP elements to
create and access the object. For example, the Duke's Bookstore pages bookdetails.jsp, catalog.jsp, and
showcart.jsp replace the scriptlet with the much more concise JSP useBean element:
<jsp:useBean id="beanName" class="fully_qualified_classname" scope="scope"/>
Or
<jsp:useBean id="beanName" class="fully_qualified_classname“
scope="scope"> <jsp:setProperty /> </jsp:useBean>
Types of properties
• Simple Properties
- private String name
• Indexed properties
- private String[] skills
• Boolean properties
- private boolean experienced
How to access javabean in JSP
• <jsp:useBean> action
- Enables JSP to manipulate Java object
• Creates Java object or locates an existing object for use in JSP
• Location can be
- Page Scope
- Request Scope
- Session Scope
- Application Scope
<jsp:useBean> attributes
Attribute Description
id The name used to manipulate the Java object with actions
<jsp:setProperty> and <jsp:getProperty>. A variable of
this name is also declared for use in JSP scripting elements. The name
specified here is case sensitive.
scope The scope in which the Java object is accessible—page, request,
session or application. The default scope is page.
class The fully qualified class name of the Java object.
beanName The name of a bean that can be used with method instantiate of
class java.beans.Beans to load a JavaBean into memory.
type The type of the JavaBean. This can be the same type as the class
attribute, a superclass of that type or an interface implemented by that
type. The default value is the same as for attribute class. A
ClassCastException occurs if the Java object is not of the type
specified with attribute type.
<jsp:useBean> attributes( cont)
Attribute Description
name The ID of the JavaBean for which a property (or properties) will be
set.
property The name of the property to set. Specifying "*"for this attribute
causes the JSP to match the request parameters to the properties of the
bean.For each request parameter that matches (i.e., the name of the
request parameter is identical to the bean’s property name), the
corresponding property in the bean is set to the value of the parameter.
If the value of the request parameter is"", the property value in the
bean remains unchanged.
param If request parameter names do not match bean property names, this
attribute can be used to specify which request parameter should be
used to obtain the value for a specific bean property. This attribute is
optional. If this attribute is omitted, the request parameter names must
match bean property names.
value The value to assign to a bean property. The value typically is the result
of a JSP expression. This attribute is particularly useful for setting
bean properties that cannot be set using request parameters. This
attribute is optional. If this attribute is omitted, the JavaBean property
must be of a type that can be set using request parameters.
Fig. 25.16 Attributes of the <jsp:setProperty> action.
JavaBean Example
package mypack;
public class Student {
private String name;
private int rollno;
public Student(){}
public Student(String name, int rollno) {
this.name = name;
this.rollno = rollno; }
public String getName() { return name; }
public void setName(String name) {this.name = name; }
public int getRollno() { return rollno; }
public void setRollno(int rollno) { this.rollno = rollno; }
}
Setting the properties
<jsp:useBean id=“student" class=“mypack.Student" scope="session">
<jsp:setProperty name=“student" property=“name“ value=“king” />
<jsp:setProperty name=“student” property=“rollno” value=“21” />
</jsp:useBean>
1. beanName must be the same as that specified for the id
attribute in a useBean element.
2. There must be a setPropName method in the JavaBeans
component.
3. paramName must be a request parameter name.
Setting the properties
Value Source Element Syntax
String constant
<jsp:setProperty name="beanName"
property="propName" value="string constant"/>
Request parameter
<jsp:setProperty name="beanName"
property="propName" param="paramName"/>
Request parameter name matches
bean property
<jsp:setProperty name="beanName"
property="propName"/>
<jsp:setProperty name="beanName"
property="*"/>
Expression
<jsp:setProperty name="beanName"
property="propName"
value="<%= expression %>"/>
Accessing javaBean in jsp
<html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<body>
<jsp:useBean id="s" class="mypack.Student" scope="session">
<jsp:setProperty name="s" property="name" value="ma ma"/>
<jsp:setProperty name="s" property="rollno" value="1"/>
</jsp:useBean>
<jsp:getProperty name="s" property=“name”/>
<jsp:getProperty name="s" property="rollno"/>
</body>
</html>
Advantages of JavaBean
• “Write once run anywhere”
• Reusable Components
• Seperation of Business logic from Presentation and Navigation
• Loosely Coupled
• EJB can also be used for business logic
• No need to implement any API for creating a JavaBean
• Using reflection can access properties, method easily dynamically
Summary
• JavaBean is a specification
• Simple convention for creating JavaBean
• Best Practises
• Standard action to create and access the JavaBean
• Standard action to read their content
• EL can also be used to access the properties

More Related Content

PDF
Lap trinh web [Slide jsp]
PPTX
Spring Web Views
PDF
JSP Syntax_1
PPTX
Java beans
PDF
13 java beans
PPTX
Hibernate inheritance and relational mappings with examples
PDF
J2EE jsp_01
PPTX
Java beans
Lap trinh web [Slide jsp]
Spring Web Views
JSP Syntax_1
Java beans
13 java beans
Hibernate inheritance and relational mappings with examples
J2EE jsp_01
Java beans

What's hot (20)

PPTX
Implicit object.pptx
PPT
Java beans
PPT
Introduction to java beans
PPTX
Implicit objects advance Java
PDF
Jsp quick reference card
PDF
Hibernate
PPTX
Javabeans
PDF
Java Web Programming [7/9] : Struts2 Basics
PDF
Service Oriented Architecture -Unit II - Modeling databases in xml
PDF
Lecture 9 - Java Persistence, JPA 2
PPTX
Java component
PPTX
Jsp presentation
PDF
Javabeans .pdf
PPT
3.jsp tutorial
PDF
A Dexterity Intro for Recovering Archetypes Addicts
PPTX
Hibernate
PDF
RequireJS & Handlebars
PPTX
Integration of Backbone.js with Spring 3.1
PPTX
Jsp Introduction Tutorial
Implicit object.pptx
Java beans
Introduction to java beans
Implicit objects advance Java
Jsp quick reference card
Hibernate
Javabeans
Java Web Programming [7/9] : Struts2 Basics
Service Oriented Architecture -Unit II - Modeling databases in xml
Lecture 9 - Java Persistence, JPA 2
Java component
Jsp presentation
Javabeans .pdf
3.jsp tutorial
A Dexterity Intro for Recovering Archetypes Addicts
Hibernate
RequireJS & Handlebars
Integration of Backbone.js with Spring 3.1
Jsp Introduction Tutorial
Ad

Similar to Advance java session 12 (20)

PPTX
Session 4 - Understanding JAVA Beans.pptx
PDF
Using java beans(ii)
PPT
Jsp Presentation +Mufix "3"
PDF
java beans
PPTX
Jsp session 6
PPTX
Learning jsp
PPT
JSP Part 2
PPTX
Codename BEAN.pptx
PDF
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
DOCX
Java beans
PPT
introduction of Java beans
PPTX
JAVA_BEAN.pptx
PPTX
Jsp session 5
DOCX
PPTX
Java Beans Unit 4(Part 1)
PPTX
4. jsp
PPTX
Unit4wt
PPT
Spring talk111204
PPTX
Unit4wt
Session 4 - Understanding JAVA Beans.pptx
Using java beans(ii)
Jsp Presentation +Mufix "3"
java beans
Jsp session 6
Learning jsp
JSP Part 2
Codename BEAN.pptx
WEB PROGRAMMING UNIT IV NOTES BY BHAVSINGH MALOTH
Java beans
introduction of Java beans
JAVA_BEAN.pptx
Jsp session 5
Java Beans Unit 4(Part 1)
4. jsp
Unit4wt
Spring talk111204
Unit4wt
Ad

More from Smita B Kumar (20)

PPTX
Advance java session 20
PPTX
Advance java session 19
PPTX
Advance java session 18
PPTX
Advance java session 17
PPTX
Advance java session 16
PPTX
Advance java session 15
PPTX
Advance java session 14
PPTX
Advance java session 13
PPTX
Advance java session 11
PPTX
Advance java session 10
PPTX
Advance java session 9
PPTX
Advance java session 8
PPTX
Advance java session 7
PPTX
Advance java session 6
PPTX
Advance java session 5
PPTX
Advance java session 4
PPTX
Advance java session 3
PPTX
Advance java session 2
PPTX
JEE session 1
PPTX
01 introduction to struts2
Advance java session 20
Advance java session 19
Advance java session 18
Advance java session 17
Advance java session 16
Advance java session 15
Advance java session 14
Advance java session 13
Advance java session 11
Advance java session 10
Advance java session 9
Advance java session 8
Advance java session 7
Advance java session 6
Advance java session 5
Advance java session 4
Advance java session 3
Advance java session 2
JEE session 1
01 introduction to struts2

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
KodekX | Application Modernization Development
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Big Data Technologies - Introduction.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Empathic Computing: Creating Shared Understanding
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Cloud computing and distributed systems.
PDF
Unlocking AI with Model Context Protocol (MCP)
cuic standard and advanced reporting.pdf
Approach and Philosophy of On baking technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The AUB Centre for AI in Media Proposal.docx
Review of recent advances in non-invasive hemoglobin estimation
KodekX | Application Modernization Development
Encapsulation_ Review paper, used for researhc scholars
Big Data Technologies - Introduction.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Network Security Unit 5.pdf for BCA BBA.
Advanced methodologies resolving dimensionality complications for autism neur...
Reach Out and Touch Someone: Haptics and Empathic Computing
NewMind AI Weekly Chronicles - August'25 Week I
Empathic Computing: Creating Shared Understanding
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Understanding_Digital_Forensics_Presentation.pptx
Cloud computing and distributed systems.
Unlocking AI with Model Context Protocol (MCP)

Advance java session 12

  • 2. Agenda • Understanding the benefits of beans • Creating beans • Installing beans classes on server • Accessing bean properties
  • 3. What is JavaBean • JavaBean is reusable software components • Rules for creating Java Bean - Public class - Private/protected instance variable - Zero-arg constructor - Public getter and setter methods(accessor and mutator ) • Any java class which satisfies the above rule is a Java Bean
  • 4. Why use a JavaBeans Component • A JSP page can create and use any type of Java programming language object within a declaration or scriptlet. The following scriptlet creates the bookstore shopping cart and stores it as a session attribute: - <% ShoppingCart cart = (ShoppingCart)session. getAttribute("cart"); // If the user has no cart, create a new one if (cart == null) { cart = new ShoppingCart(); session.setAttribute("cart", cart); } %> • If the shopping cart object conforms to JavaBeans conventions, JSP pages can use JSP elements to create and access the object. For example, the Duke's Bookstore pages bookdetails.jsp, catalog.jsp, and showcart.jsp replace the scriptlet with the much more concise JSP useBean element: <jsp:useBean id="beanName" class="fully_qualified_classname" scope="scope"/> Or <jsp:useBean id="beanName" class="fully_qualified_classname“ scope="scope"> <jsp:setProperty /> </jsp:useBean>
  • 5. Types of properties • Simple Properties - private String name • Indexed properties - private String[] skills • Boolean properties - private boolean experienced
  • 6. How to access javabean in JSP • <jsp:useBean> action - Enables JSP to manipulate Java object • Creates Java object or locates an existing object for use in JSP • Location can be - Page Scope - Request Scope - Session Scope - Application Scope
  • 7. <jsp:useBean> attributes Attribute Description id The name used to manipulate the Java object with actions <jsp:setProperty> and <jsp:getProperty>. A variable of this name is also declared for use in JSP scripting elements. The name specified here is case sensitive. scope The scope in which the Java object is accessible—page, request, session or application. The default scope is page. class The fully qualified class name of the Java object. beanName The name of a bean that can be used with method instantiate of class java.beans.Beans to load a JavaBean into memory. type The type of the JavaBean. This can be the same type as the class attribute, a superclass of that type or an interface implemented by that type. The default value is the same as for attribute class. A ClassCastException occurs if the Java object is not of the type specified with attribute type.
  • 8. <jsp:useBean> attributes( cont) Attribute Description name The ID of the JavaBean for which a property (or properties) will be set. property The name of the property to set. Specifying "*"for this attribute causes the JSP to match the request parameters to the properties of the bean.For each request parameter that matches (i.e., the name of the request parameter is identical to the bean’s property name), the corresponding property in the bean is set to the value of the parameter. If the value of the request parameter is"", the property value in the bean remains unchanged. param If request parameter names do not match bean property names, this attribute can be used to specify which request parameter should be used to obtain the value for a specific bean property. This attribute is optional. If this attribute is omitted, the request parameter names must match bean property names. value The value to assign to a bean property. The value typically is the result of a JSP expression. This attribute is particularly useful for setting bean properties that cannot be set using request parameters. This attribute is optional. If this attribute is omitted, the JavaBean property must be of a type that can be set using request parameters. Fig. 25.16 Attributes of the <jsp:setProperty> action.
  • 9. JavaBean Example package mypack; public class Student { private String name; private int rollno; public Student(){} public Student(String name, int rollno) { this.name = name; this.rollno = rollno; } public String getName() { return name; } public void setName(String name) {this.name = name; } public int getRollno() { return rollno; } public void setRollno(int rollno) { this.rollno = rollno; } }
  • 10. Setting the properties <jsp:useBean id=“student" class=“mypack.Student" scope="session"> <jsp:setProperty name=“student" property=“name“ value=“king” /> <jsp:setProperty name=“student” property=“rollno” value=“21” /> </jsp:useBean> 1. beanName must be the same as that specified for the id attribute in a useBean element. 2. There must be a setPropName method in the JavaBeans component. 3. paramName must be a request parameter name.
  • 11. Setting the properties Value Source Element Syntax String constant <jsp:setProperty name="beanName" property="propName" value="string constant"/> Request parameter <jsp:setProperty name="beanName" property="propName" param="paramName"/> Request parameter name matches bean property <jsp:setProperty name="beanName" property="propName"/> <jsp:setProperty name="beanName" property="*"/> Expression <jsp:setProperty name="beanName" property="propName" value="<%= expression %>"/>
  • 12. Accessing javaBean in jsp <html> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <body> <jsp:useBean id="s" class="mypack.Student" scope="session"> <jsp:setProperty name="s" property="name" value="ma ma"/> <jsp:setProperty name="s" property="rollno" value="1"/> </jsp:useBean> <jsp:getProperty name="s" property=“name”/> <jsp:getProperty name="s" property="rollno"/> </body> </html>
  • 13. Advantages of JavaBean • “Write once run anywhere” • Reusable Components • Seperation of Business logic from Presentation and Navigation • Loosely Coupled • EJB can also be used for business logic • No need to implement any API for creating a JavaBean • Using reflection can access properties, method easily dynamically
  • 14. Summary • JavaBean is a specification • Simple convention for creating JavaBean • Best Practises • Standard action to create and access the JavaBean • Standard action to read their content • EL can also be used to access the properties