x

JSP Expressions

PREV     NEXT

JSP expressions are very powerful. They are evaluated by JSP container and values are printed on the web page. They allow display of dynamic contents on the web page. The expression to be evaluated is placed between <%= and %>.The equivalent XMP syntax for JSP expression is


<jsp:expression> jsp expression here </jsp:expression>

The example below first declares an integer i using JSP declarations and sets it to zero. It then uses JSP expression to print i, the square of i and 2i. After this i is incremented by 1. The same statements are then repeated two more times.


<%! int i %> <table>
<tr>
<th>number</th>
<th>square</th>
<th>power of two</th>
</tr>
<tr>
<td><%=i%></td>
<td><%=i*i%></td>
<td><%= java.lang.Math.pow(2,i++)%></td>
</tr>
<tr>
<td><%=i%></td>
<td><%=i*i%></td>
<td><%= java.lang.Math.pow(2,i++)%></td>
</tr>
<tr>
<td><%=i%></td>
<td><%=i*i%></td>
<td><%= java.lang.Math.pow(2,i++)%></td>
</tr>
</table>

The expression <%=i%> prints the value of i. The expression <%=i*i%>prints the square of i. The expression <%= java.lang.Math.pow(2,i++)%>prints 2i, and then increments i.

The following is dispayed on the web page as output from above page –

number    square      power of two
0              0                  1.0
1              1                  2.0
2              4                  4.0

PREV     NEXT



Like it? Please Spread the word!