How do I use expression language with JSTL?
Author: Deron Eriksson
Description: This Java tutorial describes how to use Expression Language with JSTL on JSPs.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


If you'd like to utilize expression language (EL) in your JSTLSW, you should use the JSTL 1.1 core library tags (or later), which can be specified in your jspW like the following taglib directives example:

JSTL 1.1 Core Library Tags example (Expression Language)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

Notice that the 1.1 core library tags have /jsp/ in the uri. If you use the 1.0 core library tags, notice that there is no /jsp/ in the uri.

Here is an example of using expression language with the JSTL 1.1 Core Library Tags.

<c:set var="testing" value="blah"/>
<c:out value="${testing}"/>

The expression is evaluated, and the result is displayed:

jstl-test.jsp displayed in web browser

Next, I'll show what happens if you try to use expression language with the JSTL 1.0 Core Library Tags.

JSTL 1.0 Core Library Tags example (No Expression Language)

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jstl/sql" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>

With the JSTL 1.0 Core Library Tags, if you try using expression language, as in the following example (in the c:out):

<c:set var="testing" value="blah"/>
<c:out value="${testing}"/>

You will be greeted by an error message, such as the following:

org.apache.jasper.JasperException: /jstl-test.jsp(7,0) According to TLD or attribute directive in tag file,
attribute value does not accept any expressions
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:512)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

So, utilize the JSTL 1.1 Core Library Tags (or later) if you'd like to use expression language with your JSTL tags.