# jsp:setProperty Tag

The <jsp:setProperty> tag sets the value of one or more properties in a bean, using the bean's setter methods. You must declare the bean with <jsp:useBean> before you set a property value with <jsp:setProperty>. Because <jsp:useBean> and <jsp:setProperty> work together, the bean instance names they use must match. That is, the value of name in <jsp:setProperty> and the value of id in <jsp:useBean> must be the same.

You can use <jsp:setProperty> to set property values in several ways:

- By passing all of the values the user enters (stored as parameters in the request object) to matching properties in the bean
- By passing a specific value the user enters to a specific property in the bean
- By setting a bean property to a value you specify as either a String or an expression that is evaluated at runtime

For more information, see the JavaServer Pages (JSP) v1.2 Syntax Reference on the Sun Microsystems® web site.

## Syntax

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

## Attributes

name="beanInstanceName"

The name of an instance of a bean that has already been created or located with a <jsp:useBean> tag. The value of name must match the value of id in <jsp:useBean>. The <jsp:useBean> tag must appear before <jsp:setProperty> in the JSP page.

property="*"

Stores all of the values of request parameters in bean properties. The names of the bean properties must match the names of the request parameters. A bean property is usually defined by a variable declaration with matching getter and setter methods. When you use property="*", the bean properties are not necessarily set in the order in which they appear in the HTML form or the bean. If the order in which the properties are set is important to how your bean works, use the syntax form property="propertyName" [ param="parameterName" ]. Or rewrite your bean so that the order of setting properties is not important.

property="propertyName" [ param="parameterName" ]

Sets one bean property to the value of one request parameter. In the syntax, property specifies the name of the bean property and param specifies the name of the request parameter by which data is being sent from the client to the server. If the bean property and the request parameter have different names, you must specify both property and param. If they have the same name, you can specify property and omit param. If a parameter has an empty or null value, the corresponding bean property is not set.

property="propertyName" value="{ String | <%= expression %>}"

Sets one bean property to a specific value. The value can be a String or an expression that is evaluated at runtime. If the value is a String, it is converted to the bean property's data type according to the conversion rules shown above in TABLE 1. If it is an expression, its value must have a data type that matches the the data type of the value of the expression must match the data type of the bean property. If the parameter has an empty or null value, the corresponding bean property is not set. You cannot use both the param and value attributes in a <jsp:setProperty> tag.

## Examples

    <jsp:setProperty name="mybean" property="*" />
    <jsp:setProperty name="mybean" property="username" />
    <jsp:setProperty name="mybean" property="username" value="Steve" />

Related Topics

jsp:fallback Tag

jsp:forward Tag

jsp:getProperty Tag

jsp:include Tag

jsp:param Tag

jsp:params Tag

jsp:plugin Tag

jsp:useBean Tag

JSP Tag Reference for Page Flows