We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
Using Beans in JSP PagesUsing Beans in JSP Pages
* A Java Bean is a specially constructed Java class written in the
Java.
* Java Beans are java classes that have properties, which can be
read via a get method or changed via a set method.
* A Java Bean should not have any public variables. All the
variables should be accessed using the get/set methods.
JavaBeans Properties
* A JavaBean property is a named attribute that can be
accessed by the object. The attribute can be of any Java data
type, including the classes that you define.
* JavaBean properties are accessed through two methods in
the JavaBean's implementation class.Using Beans in JSP Pages
getPropertyName()
* For example, if property name is firstName, your method
name would be getFirstName() to read that property.
setPropertyName()
* For example, if property name is firstName, your method
name would be setFirstName() to write that property.
Accessing JavaBeans
* Following JSP action elements are required to use Java
bean in a JSP file.
—
—
— Load Java bean inside a JSP :
* We need to use tag to load a bean into JSP.
* The basic syntax is given below:
Getting the Properties of the Bean:
« After the bean gets loaded into the page, the properties can
be accessed by using the action element.
* The basic syntax is given below:
Setting the Properties of the Bean :
* To modify (or) assign value to any variable of the bean object
the action element is used .
* The basic syntax is as below
Example: “Employee.java”
Step 1: Create Java Bean
package college;
public class Employee {
private String name = "Madhu";
private String department = "CSE";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}Example: “jbeandemo.jsp”
Step 2: Create JSP
Java Bean Demo