Learn Java for Web Development

(Tina Meador) #1
CHAPTER 2: Building Web Applications Using Servlets and JSP 85

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 action has the following syntax:



The action sets the properties of a bean.


The action has the following syntax where someId is the ID of the useBean:


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


The 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 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.
2.
3.
4.
5.
6.
7.Hello 
8.
9.

Free download pdf