SoFunction
Updated on 2025-04-07

JSP Tutorial (V) - Use of JSP Actions

jsp:useBean Action usage

1. Syntax:
<jsp:useBean

scope="page|request|session|application"
{ class="" |
type="" |
class="" type="" |
beanName="{ | <%= expression %>}" type=""
}
{ /> |
> Other elements
</jsp:useBean>
}

This action allows you to load a JavaBean into a JSP page. This is a very useful capability because it allows you to use reusable JAVA classes without sacrificing performance. The simplest syntax is used to specify a bean:

<jsp:useBean class="" />

This usually means "instance an object of a class by specifying a class and binding it with a variable whose name is specified by id". However, as we see, you can specify a scope property to make the bean more than just the current page. In this case, it is very useful to get a reference to an existing bean, and to create a new one only if the bean with the same id and scope exists. Now that you have the bean, you can modify it via jsp:setProperty, or, by using the name specified with the id, to use the scriptlet or explicit call method. When you say "this bean has an X-type property called foo", what you really mean is "this class has a method called getFoo, which returns a certain type of value of type X, and another method called setFoo, which takes X as a parameter." This jsp:setProperty action will be introduced in detail in the next unit, but now you can either give a clear value and give a property to indicate that this value is inherited from the parameter of the request, or you can simply list the properties to indicate that this value should be inherited from the parameter of the property name. You can get an existing JSP expression or scriptlet property by calling the applicable getXxx method, or more generally, using jsp:getProperty action.

Note that the class specified for the bean must be under the classpath of the server's rule, rather than to retain the path of the class that is automatically loaded when changed. For example, on Java Web Server, it and the classes it uses must go to the class directory or a jar file in the lib directory, rather than in the servlets directory.

Let's look at a very simple example, which loads a bean and sets/gets a simple string parameter.


<HTML>
<HEAD>
<TITLE>Reusing JavaBeans in JSP</TITLE>
</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">
Reusing JavaBeans in JSP</TABLE>
</CENTER>
<P>
<jsp:useBean class="" />
<jsp:setProperty name="test"
property="message"
value="Hello WWW" />
<H1>Message: <I>
<jsp:getProperty name="test" property="message" />
</I></H1>
</BODY>


Here is the original code of the bean:

package hall;
public class SimpleBean {
private String message = "No message specified";
public String getMessage() {
return(message);
}
public void setMessage(String message) {
= message;
}
}

The running result is: Page output: Reusing JavaBeans in JSP

b>Message:Hello WWW

2. Detailed usage of jsp:useBean

The easiest way to use beans is:

<jsp:useBean class=""/>
In order to load a bean, you need to use jsp:setProperty and jsp:getProperty to modify and retrieve the properties of the bean. And, there are two other options. First, you can use the format of the container, that is:

<jsp:useBean ...>
Body
</jsp:useBean>

It should be noted that the Body part should only be executed when the bean is instantiated for the first time, not every time it is found and used. Beans can be shared, so not all jsp:useBean statements produce a new instance of bean. Secondly, in addition to id or class, there are three properties you can use: scope, type, and beanName. These properties are summarized as follows:

property

usage

id
Give a variable name, which will point to the bean. If you find that there is a bean with the same id and scope, use it instead of creating a new one.

class
Specify the complete package name of the bean.

scope
Indicates the relationship between the beans that can be used above. There are four possible values: page, request, session, and application. The default is page, indicating that the bean is only available on the current page (save in the current PageContext). A value of request indicates that the bean is only used for the request of the current client (save in the ServletRequest object). The value of the Session indicates that the object is available to all pages during the current HttpSession lifecycle. Finally, the application value indicates that the object can be used for all pages that share the ServletsContext. Use jsp:useBean to create a new bean only if there is no bean with the same id and scope, and use it if there is already, and ignore the code starting and ending with the jsp:useBean flag.

type
Specifies the type of variable to point to the object. This must match the class name or be a superclass or an interface to implement the class. Remember, the name of the variable is specified by the id attribute.

beanName
Give the bean a name, which you should provide in the instantiation method of the beans. It allows you to give type and a beanName, and omit the class attributes.
3. jsp:setProperty Action

grammar:

<jsp:setProperty
name="beanInstanceName"
{ property="*" |
property="propertyName" [ param="parameterName" ] |
property="propertyName" value="{string | <%= expression %>}"
}
/>

We already know that we can use jsp:setProperty to assign values ​​to a bean's properties. There are two ways you can implement it. One is to use jsp:setProperty after (not inside) after jsp:useBean:

<jsp:useBean ... />
...
<jsp:setProperty name="myName"
property="someProperty" ... />

In this way, jsp:setProperty will be executed regardless of whether there is a bean with the same id and scope already exists. Another way is that jsp:setProperty appears in the jsp:useBean element, such as:

<jsp:useBean ... >
...
<jsp:setProperty name="myName"
property="someProperty" ... />
</jsp:useBean>

In this case, jsp:setProperty is executed only when the new object is instantiated.

Here are four available properties for jsp:setProperty:

property

usage

name
This is a required property. It indicates which bean's properties will be set. jsp:usebean must appear before jsp:setProperty.

property
This is a required property. Indicates which attribute you will set. However, there is a special case: if the value of "*" means that all request parameters whose names match the properties of the bean will be passed to the corresponding property setting method.
value
This is an optional property. It specifies the value of the property to be set. The value of the string is automatically converted to numbers, boolean, Boolean, byte, Byte, char, and Character through the standard valueOf method of the corresponding object or package. For example, the value "true" of the boolean or Boolean attribute will be converted by the method, while the value "42" of an int or Integer attribute will be converted by the conversion. You cannot use both value and param attributes, but neither is allowed.

param
This is an optional property. It specifies the request parameters that the bean's properties should inherit. If the current request does not have such a parameter, nothing is done: the system does not pass null to the method that sets the property. Therefore, you can use the default value of the bean. For example, the following program executes "Set the numberOfItems property to the value of any numItems request parameter, if there is such a request parameter, otherwise do nothing."

<jsp:setProperty name="orderBean"
property="numberOfItems"
param="numItems" />

If you default value and param at the same time, this is the same as if you set the name of param to the property name of bean. You can automatically use the request attribute corresponding to the bean's properties by setting the value of name to "*" and omitting the value and param. In this case, the server will repeatedly look up available properties and request parameters to match those with the same name.

4. jsp:getProperty Action

grammar:

<jsp:getProperty name="beanInstanceName" property="propertyName" />

This property retrieves the value of the bean's property and converts it into a string, and then inserts it into the output. It has two required properties: name, the name introduced with jsp:useBean, property, the property that must be inserted into the value.