Java Server Pages - EL and JSTL
As previously mentioned, scriptlets are largely outmoded. EL and JSTL can be used for many of the same functions.
This post will discuss:
- Expression Language
- JSTL
Expression Language
Expression Language directly replaces expression tags. It is written in syntax:
${func.}
EL supports the following types:
- Primitives
- Parameters, of defined Scope or otherwise
- Implicit objects in pageContext
- Methods within Scope
Most common functions, declarations, initializations, etc. cannot be executed within EL tags. EL should only be used to set/get data and execute methods that return data. Other logic should be executed within beans, then offloaded to the view component.
Java Standard Tag Library
JSTL provides a set of HTML-like tags that provide Java Pages functionality within a JSP, without directly placing Java logic in a JSP document. The tags are as follows:
Output a value:
<c:out value="BJ"/>
Set a variable usable by JSTL tags within the document.
<c:set var="OBJ" value="OBJ" scope="SCOPE"/>
Remove a previously created variable.
<c:remove var="OBJ" scope="SCOPE"/>
If statement. If the condition is true, code within the tags is parsed.
<c:if test="CONDITION" var="OBJ"></c:if>
Switch case. If the condition for the case is true, code within the tags is parsed.
<c:choose><c:when test="CONDITION"></c:when></c:choose>
For statement. If the condition is true, code within the tags is continuously parsed.
//Standard FOR statement
<c:forEach begin="INT" end="INT" step="INT"><c:forEach>
//List/Enum FOR statement
<c:forEach items="LIST" var="OBJ"><c:forEach>
JSTL has the above core tags, but its functionality can be extended with other components of the JSTL library, such as fn functions.
-gonkgonk