JSTL/JSP Expression Language 

Joined:
08/13/2009
Posts:
110

March 23, 2010 02:33:54    Last update: March 31, 2010 04:16:40
Expression Language (EL) was introduced since JSTL 1.0, it is part of the JSP spec since JSP 2.0/JSTL 1.1. There's no JSTL without EL.

But don't let the name scare you, EL is not another programming language. It's just a simple construct: ${expr}, which means that instead of taking expr literally, it should be evaluated in the page using any page/request/session/application variables exposed to that page.

For example, if you put
Hello ${world}!

in your JSP, world will be evaluated in the page context. If you had set a value for world before, for example,
request.setAttribute("world", "John Doe");

in your Struts action class or a servlet, the JSP page would show:
Hello John Doe!


There were confusions about EL support in various servlet/JSP/JSTL version combinations some time back, but these are no longer concerns. Everything should work if you specify servlet 2.4 or 2.5:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
	 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	 version="2.5">
    <display-name>Simple JSTL Demo Application</display-name>
    <servlet>
        <servlet-name>jstldemo</servlet-name>
        <servlet-class>jstl.demo.JSTLDemoServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jstldemo</servlet-name>
        <url-pattern>/demo</url-pattern>
    </servlet-mapping>
</web-app>


Escaping an EL expression
What to do if I want to write out a literal string like ${no_expression_evluation}? You escape it like this: ${'${'}no_expression_evluation}.
Share |
| Comment  | Tags