tag
The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a scripting variable that can be accessed by both scripting elements and other custom tags used in the JSP.

JSP Syntax

<jsp:useBean
        id="beanInstName"
        scope="page | request | session | application"       
        class="package.class"    
        type="package.class"
</jsp:useBean>

Example

  <jsp:useBean id="stock" scope="request" class="market.Stock" />

Accessing Values

<jsp:useBean id="date" class="java.util.Date" /> 
<p>The date/time is <%= date %>

Using getPropertyName() and setPropertyName()

<jsp:useBean id="students" 
                    class="com.tutorialspoint.StudentsBean"> 
   <jsp:setProperty name="students" property="firstName"
                    value="Zara"/>   
</jsp:useBean>

<p>Student First Name: 
   <jsp:getProperty name="students" property="firstName"/>
</p>

Setting Request Scope

PersonBean myBean = (PersonBean)request.getAttribute("myBean");

if(myBean == null)
{
   myBean = new PersonBean();
   request.setAttribute("myBean", myBean);
}

Accessing in JSP

<jsp:useBean id="myBean" class="PersonBean" scope="request" />
Posted in JSP.

Comments are closed.