Java Bean Content Beyond
Java Bean Content Beyond
It should be Serializable.
It should provide methods to set and get the values of the properties, known as
getter and setter methods.
To access the java bean class, we should use getter and setter methods.
1. package mypack;
2. public class Test{
3. public static void main(String args[]){
4.
5. Employee e=new Employee();//object is created
6.
7. e.setName("Arjun");//setting value to the object
8.
9. System.out.println(e.getName());
10.
11. }}
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 Description
JavaBeans Example:
Consider a student class with few properties:
package com.tutorialspoint;
public StudentsBean() {
return firstName;
return lastName;
return age;
this.firstName = firstName;
this.lastName = lastName;
}
this.age = age;
Accessing JavaBeans:
The useBean action declares a JavaBean for use in a JSP. Once declared,
the bean becomes a scripting variable that can be accessed by both
scripting elements and other custom tags used in the JSP. The full syntax
for the useBean tag is as follows:
Here values for the scope attribute could be page, request, session or
application based on your requirement. The value of the id attribute may be
any value as a long as it is a unique name among other useBean
declarations in the same JSP.
<html>
<head>
<title>useBean Example</title>
</head>
<body>
</body>
</html>
value="value"/>
...........
</jsp:useBean>
<html>
<head>
</head>
<body>
<jsp:useBean id="students"
class="com.tutorialspoint.StudentsBean">
value="Zara"/>
value="Ali"/>
value="10"/>
</jsp:useBean>
<p>Student First Name:
</p>
</p>
<p>Student Age:
</p>
</body>
</html>
Student Age: 10