JavaBean class in Java

JavaBeans are classes that encapsulate many objects into a single object (the bean). It is a java class that should follow following conventions:

  1. Must implement Serializable.
  2. It should have a public no-arg constructor.
  3. 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

  1. All properties private (use getters/setters)
  2. A public no-argument constructor
  3. 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

  1. id: is used to identify the bean in the specified scope.
  2. 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.
  3. 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.
  4. 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.
  5. 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;
	}
}

  1. 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>
  1. 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>
  1. The window of the program is as following:

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值