Java Reference
In-Depth Information
The
useBean
action is used to declare and initialize the bean object. Once the bean is initialized,
you can use the
jsp:setProperty
and
jsp:getProperty
actions to set and get bean properties
The
<jsp:useBean>
action has the following syntax:
<jsp:useBean id="someId" class="SomeClass" />
The
<jsp:setProperty>
action sets the properties of a bean.
The
<jsp:setProperty>
action has the following syntax where
someId
is the ID of the
useBean
:
<jsp:setProperty name="someId" property="someProperty" .../>
The
<jsp: getProperty>
action, as the name suggests, gets the value of a given property. If the
property is not a string, it converts it to a string.
The
<jsp: getProperty>
action has the following syntax where
someId
is the ID of the
useBean
:
<jsp:getProperty name="someId" property="someProperty" .../>
Listing 2-25 shows how to create a user bean, and Listing 2-26 shows the usage of these three
actions in a JSP page.
Listing 2-25. User Bean
1.package com.apress.jspactions;
2.
3.public class User {
4.
5.private String name;
6.
7.public String getName() {
8.return name;
9.}
10.
11.public void setName(String name) {
12.this.name = name;
13.}
14.
15.}
The user bean in Listing 2-25 will be used in
user.jsp
, which is shown in Listing 2-26.
Listing 2-26. user.jsp
1.<html>
2.<head>
3.</head>
4.<body>
5.<jsp:useBean id="user" class="com.apress.jspactions.User" />
6.<jsp:setProperty name="user" property="name" value="vishal" />
7.Hello <jsp:getProperty name="user" property="name" />
8.</body>
9.</html>