JavaBeans are classes that encapsulate many objects into a single object (the bean). It is a java class that should follow following conventions:
- Must implement Serializable.
- It should have a public no-arg constructor.
- All properties in java bean must be private with public getters and setter methods.
// Java Program of JavaBean class
package geeks;
public class Student implements java.io.Serializable {
private int id;
private String name;
public Student() {
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
A JavaBean is just a standard
- All properties private (use getters/setters)
- A public no-argument constructor
- Implements Serializable.
We can set and get the property in the JavaBeans
The follow is the whole dynamic program’s layout in the Eclipse
- JavaBeans: Its just a normal java class which follow some standard
package org.studyeasy.beans;
public class User {
private String firstName;
private String lastName;
public User() {
firstName = "John";
lastName = "Doe";
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
set property using jsp page
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Set property</title>
</head>
<body>
<jsp:useBean id="user" class="org.studyeasy.beans.User" scope="session"></jsp:useBean>
<jsp:setProperty property="firstName" name="user" value="Chaand"/>
<jsp:setProperty property="lastName" name="user" value="Sheikh"/>
Values have been set
</body>
</html>
get property using jsp page
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Get property</title>
</head>
<body>
<!-- id is the javaBean's id, class is the complete path of the java class -->
<!-- here the path is org.studyeasy.beans.User -->
<jsp:useBean id="user" class="org.studyeasy.beans.User" scope="session"></jsp:useBean>
<!-- Here when we use the java class to get the property -->
<!-- the property is the class's variable name, name is the id of javaBean -->
First Name: <jsp:getProperty property="firstName" name="user"/><br/>
Last Name: <jsp:getProperty property="lastName" name="user"/>
</body>
</html>
'
Attributes and Usage of jsp:useBean action tag
- id: is used to identify the bean in the specified scope.
- scope: represents the scope of the bean. It may be page, request, session or application. The default scope is page.
page: specifies that you can use this bean within the JSP page. The default scope is page.
request: specifies that you can use this bean from any JSP page that processes the same request. It has wider scope than page.
session: specifies that you can use this bean from any JSP page in the same session whether processes the same request or not. It
has wider scope than request.
application: specifies that you can use this bean from any JSP page in the same application. It has wider scope than session. - class: instantiates the specified bean class (i.e. creates an object of the bean class) but it must have no-arg or no constructor and
must not be abstract. - type: provides the bean a data type if the bean already exists in the scope. It is mainly used with class or beanName attribute. If
you use it without class or beanName, no bean is instantiated. - beanName: instantiates the bean using the java.beans.Beans.instantiate() method.
We can also use JavaBeans using web forms as following:
1. Java Beans
package org.studyeasy.beans;
public class User {
private String firstName;
private String lastName;
public User() {
firstName = "John";
lastName = "Doe";
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
- set beans property with form
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Set property</title>
</head>
<body>
<jsp:useBean id="user" class="org.studyeasy.beans.User" scope="session"></jsp:useBean>
<form action="getSessionProperty.jsp" method="post">
First name: <input type="text" name="firstName" value='<jsp:getProperty property="firstName" name="user"/>'><br/>
Last name: <input type="text" name="lastName" value='<jsp:getProperty property="lastName" name="user"/>'><br/>
<input type="submit" value="submit">
</form>
</html>
- get beans property using getPeoperty()
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Get property</title>
</head>
<body>
<jsp:useBean id="user" class="org.studyeasy.beans.User" scope="session"></jsp:useBean>
<jsp:setProperty property="*" name="user"/>
First Name: <jsp:getProperty property="firstName" name="user"/><br/>
Last Name: <jsp:getProperty property="lastName" name="user"/>
</body>
</html>
- The window of the program is as following: