Using JSP Expressions with Example

The following example illustrates using JSP expressions.
An expression is a combination of symbols that represents a value. Examples of expression include a+b, a*b, new Date() etc.
In JSP you can get an expression value and print it without the need of writing in scriptlets. The syntax is as follows

<%=some-expression%>

Here the result of this expression is returned as String object. If the tag alone is written in a JSP page, then the expression value is printed. In simple words, you can get the result of a function call, an arithmetic operation or an object creation using the expression.

The expression shouldn't return a void value.

index.jsp


<html>
    <body>
    <%="This is sample text"%><br/>
    <%=10+20%><br/>
    <%=new Integer(20)%><br/>
    <%="The difference is"+(20-10)%>
   
    <%--
    This is an error, void type not allowed
    <%=out.println("hai")%>
    --%>
    </body>
</html>

Output of the program


This is sample text
30
20
The difference is10

Previous: <jsp:forward>

References

http://docs.oracle.com/javaee/5/tutorial/doc/bnaov.html

No comments: