[ Team LiB ] Previous Section Next Section

Basic Syntax

Typically, when you specify an attribute value in a JSP tag, you simply use a string. For example,


<jsp:setProperty name="order" property="subtotal" value="100"/>

JSP EL allows you to specify an expression for any of these attribute values. A JSP EL expression takes the form ${expr} where expr is the expression itself. You might rewrite the previous <jsp:setProperty> tag with an expression like


<jsp:setProperty name="order" property="subtotal"
    value="${product.price+shipping.price}"/>

When the JSP compiler sees the ${} form in an attribute, it generates code to evaluate the expression. The result of the expression becomes the new attribute value (the expression is evaluated at runtime, of course, not compile time).

You can also use JSP EL expressions within template text for a tag. For example, the <jsp:text> tag simply inserts its content within the body of a JSP. The following <jsp:text> declaration inserts <h1>Hello World!</h1> into the JSP output:


<jsp:text>
  <h1>Hello World!</h1>
</jsp:text>

You can include a JSP EL expression in the body of a <jsp:text> tag (or any other tag) with the same ${} syntax you use for attributes. For example,


<jsp:text>
   Your subtotal is: ${product.price + shipping.price}
</jsp:text>
    [ Team LiB ] Previous Section Next Section