Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Using <jsp:setProperty>

Although the today bean is a read-only bean, many of the beans that you will use and create can be used not just to output information, but also to store and process data that is sent to the bean. Any time you want to give a bean information, you use the <jsp:setProperty> action.

Like the <jsp:getProperty> action, <jsp:setProperty> also takes name and property attributes. name refers to the instantiated bean and property refers to the name of the bean property that you are setting with the action. However, in the <jsp:setProperty> action, you also need to pass the value that you want to set for the defined bean property. First, you can use the value attribute to set the bean property. If you want to pass dynamic information, you can use <%= varName %>. In addition, the param attribute allows you to specify a request parameter to which you can set a bean property. You can use either param or value, but not both.

Often, bean properties use data types other than String values. The <jsp:setProperty> action automatically converts the primary datatypes from a passed String object by using the appropriate valueOf() method. For example, if you passed "true", the <jsp:setProperty> action would automatically apply the results of Boolean.valueOf("true") to this passed value. The bottom line is that you don't have to worry about converting data types when you're passing values to a bean.

Let's say that your Java programming team has created a bean called UserProfile. This bean allows you to store some information about site users, including the user's e-mail address and the user's login name n. Your Java architect provides you with a specification for this bean, and gets it set up on your JRun server. The bean is stored in the com.ows.Beans area, and has the properties email and username. The following is an example of how you might use the UserProfile bean:

<jsp:useBean id="UserProfile" class="com.ows.Beans.UserProfile" 
   scope="session" />
<jsp:setBean name="UserProfile" property="email" param="Email" />
<jsp:setBean name="UserProfile" property="username" param="UserName" />

This is a basic example, but it illustrates how you could set a user's e-mail address and username, as entered through an HTML form, to a session bean. Note that the param attribute is used here to specify the name of variables passed in the request object—the form variables Email and UserName—to set these bean properties to.

  • + Share This
  • 🔖 Save To Your Account