Three important tags used in JSP Bean
In JSP (JavaServer Pages), JavaBeans are reusable software components that follow specific conventions. JSP provides tags to interact with JavaBeans, allowing you to instantiate, set properties, and retrieve properties of JavaBeans within a JSP page. The three important tags used in the development of JSP Beans are:
- <jsp:useBean>
- <jsp:setProperty>
- <jsp:getProperty>
Table of Contents
1. <jsp:useBean>
The <jsp:useBean> tag is used to declare and instantiate a JavaBean in a important tags JSP page. It can also specify the scope of the bean.
Attributes:
- id: The identifier used to reference the bean in the JSP page.
- class: The fully qualified class name of the JavaBean.
- scope: The scope of the bean, which can be page, request, session, or application.
Example:
jsp
<jsp:useBean id="user" class="com.example.User" scope="session" />
This example declares and instantiates a JavaBean of the class com.example.User with the identifier user and scope session.
2. <jsp:setProperty>
The <jsp:setProperty> tag is used to set the properties of a JavaBean. It can set properties based on request parameters or specific values.
Attributes:
- name: The identifier of the bean whose property is to be set.
- property: The name of the property to be set. Use “*” to set all properties that match request parameters.
- value: The value to set for the specified property (optional if using request parameters).
Example:
jsp
<jsp:setProperty name="user" property="name" value="John Doe" />
<jsp:setProperty name="user" property="age" value="30" />
This example sets the name and age properties of the user bean to "John Doe" and 30, respectively.
Example with Request Parameters:
jsp
<jsp:setProperty name="user" property="*" />
This example sets the properties of the user bean to the values of the corresponding request parameters.
3. <jsp:getProperty>
The tag is used to retrieve the value of a property from a JavaBean and display it in the JSP page.
Attributes:
- name: The identifier of the bean from which the property value is to be retrieved.
- property: The name of the property to be retrieved.
Example:
jsp
<p>Name: <jsp:getProperty name="user" property="name" /></p>
<p>Age: <jsp:getProperty name="user" property="age" /></p>
This example retrieves and displays the name and age properties of the user bean.
Complete Example
Here is a complete example demonstrating the use of all three tags together in a JSP page.
JavaBean Class: User.java
java
package com.example;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private int age;
public User() {}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
JSP Page: user.jsp
jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<title>User Information</title>
</head>
<body>
<jsp:useBean id="user" class="com.example.User" scope="session" />
<jsp:setProperty name="user" property="name" value="John Doe" />
<jsp:setProperty name="user" property="age" value="30" />
<p>Name: <jsp:getProperty name="user" property="name" /></p>
<p>Age: <jsp:getProperty name="user" property="age" /></p>
</body>
</html>
Summary of important tags
- <jsp:useBean> : Declares and instantiates a JavaBean.
- <jsp:setProperty> : Sets properties of a JavaBean.
- <jsp:getProperty> : Retrieves and displays properties of a JavaBean.