Basic Usage of useBean

 The <jsp:useBean> tag in JavaServer Pages (JSP) is used to instantiate a JavaBean, which is a reusable component that follows the JavaBeans convention (i.e., it has a no-argument constructor, and getter and setter methods for properties). The <jsp:useBean> tag allows you to create or access a JavaBean in your JSP page, and optionally set its properties.

Here’s a breakdown of the syntax for the <jsp:useBean> tag:

jsp
<jsp:useBean id="beanName" class="fullyQualifiedClassName" scope="scope" />
  • id: The name by which the bean is referenced in the page.
  • class: The fully qualified class name of the JavaBean (e.g., com.example.MyBean).
  • scope: The scope of the bean (optional). This can be one of the following:
    • page (default) – The bean is created for the current page request.
    • request – The bean is created for the current request.
    • session – The bean is created for the current session.
    • application – The bean is created for the entire web application (ServletContext).

Example 1: Basic Usage of <jsp:useBean>

In this example, we will create a simple JavaBean called Person, instantiate it using <jsp:useBean>, and display its properties.

JavaBean (Person.java)

java
package com.example; public class Person { private String name; private int age; // No-argument constructor public Person() {} // Getter and Setter methods 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 (index.jsp)

jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %> <%@ page import="com.example.Person" %> <html> <head> <title>Using JavaBean Example</title> </head> <body> <h1>Person Details</h1> <!-- Create or access the JavaBean --> <jsp:useBean id="person" class="com.example.Person" scope="page" /> <!-- Set properties of the JavaBean --> <jsp:setProperty name="person" property="name" value="John Doe" /> <jsp:setProperty name="person" property="age" value="30" /> <!-- Get properties of the JavaBean --> <h2>Name: <jsp:getProperty name="person" property="name" /></h2> <h2>Age: <jsp:getProperty name="person" property="age" /></h2> </body> </html>

Explanation:

  • The <jsp:useBean> tag is used to create or find the Person bean.
  • The id="person" attribute defines the name by which the bean can be accessed in the JSP page.
  • The <jsp:setProperty> tag is used to set the name and age properties of the Person bean.
  • The <jsp:getProperty> tag is used to retrieve and display the properties of the Person bean.

When the index.jsp page is accessed, it will display:

makefile
Name: John Doe Age: 30

Example 2: Using <jsp:useBean> with Request Scope

In this example, we will store the Person object in the request scope, which means the bean will be available throughout the duration of the current request.

JSP Page (requestExample.jsp)

jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %> <%@ page import="com.example.Person" %> <html> <head> <title>Request Scope Example</title> </head> <body> <h1>Using JavaBean in Request Scope</h1> <!-- Create or find the JavaBean in the request scope --> <jsp:useBean id="person" class="com.example.Person" scope="request" /> <!-- Set properties of the JavaBean --> <jsp:setProperty name="person" property="name" value="Jane Smith" /> <jsp:setProperty name="person" property="age" value="25" /> <!-- Forward request to another JSP to display the bean's properties --> <jsp:forward page="displayPerson.jsp" /> </body> </html>

Displaying the Bean's Properties (displayPerson.jsp)

jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %> <html> <head> <title>Display Person</title> </head> <body> <h1>Person Details</h1> <h2>Name: <jsp:getProperty name="person" property="name" /></h2> <h2>Age: <jsp:getProperty name="person" property="age" /></h2> </body> </html>

Explanation:

  • In the requestExample.jsp page, the Person bean is created in the request scope using <jsp:useBean>.
  • We then set the properties name and age using <jsp:setProperty>.
  • The request is forwarded to displayPerson.jsp, where the properties of the Person bean are displayed using <jsp:getProperty>.

Example 3: Using <jsp:useBean> with Session Scope

If you want to store the JavaBean in the session scope (so it persists across multiple requests), you can set the scope to session.

jsp
<jsp:useBean id="person" class="com.example.Person" scope="session" /> <jsp:setProperty name="person" property="name" value="Alice" /> <jsp:setProperty name="person" property="age" value="28" />

In this case, the person object will be available in the session and can be accessed across multiple requests within the same session.


Summary

  • <jsp:useBean> is used to instantiate and manage JavaBeans in JSP pages.
  • It can create a new bean or access an existing one in various scopes (page, request, session, application).
  • The bean's properties can be set using <jsp:setProperty> and retrieved using <jsp:getProperty>.

Comments