In this example you will learn how to use the <jsp:useBean>. We can use the useBean action to create a scripting variables. To define the variable name we use the id attribute of this action. The class attribute define the type of this variable. We can also define the scope of the variable using the scope attribute.
As an example in the code snippet below we create a variable name now and declare it to hold a java.util.Date instance. And then we use a JSP expression to print out the value.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<title>JSP - useBean Demo</title>
<jsp:useBean id="now" class="java.util.Date"/>
Now is: <%= now %>
In this example you will learn how to set and get the value of Java object properties that you define in a JSP pages. For this example, let’s first start by creating a variable that we named customer, that will have a type of Customer class. To create this variable we use the <jsp:useBean> action.
After we create the customer variable we can set the property value of the customer bean using the <jsp:setProperty> action. And to get the property value of the customer bean we use the <jsp:getProperty> action.
The name attribute in the setProperty and getProperty action refer to our customer bean. The property attribute tells which property we are going to set or get. To set the value of a property we use the value attribute.