tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Thomas <>
Subject Re: Invoke methods in EL?
Date Mon, 10 Apr 2017 22:24:20 GMT
On 10/04/17 16:35, Tim Watts wrote:
> Environment: Tomcat 7.0.59; JRE 1.8.0_72
> I suspect the answer to my problem is "You can't do that" but here goes:
> A simple JSP that tries to get a Calendar instance and outputs the year:
> <%@ page language="java" 
>     contentType="text/html; 
>     charset=UTF-8" pageEncoding="UTF-8"%>
> <%@ taglib prefix="c" uri="" %>
> <!DOCTYPE html>
> <html>
> <body>
> 	<c:set var="X" value="${ java.util.Calendar.getInstance() }"/>
> 	<c:set var="Y" value="${ X.get(java.util.Calendar.YEAR) }"/>
> 	The year is ${ Y }
> </body>
> </html>
> This presents a page that just says "The year is" but no year value.  Is
> there some syntactical mojo I'm missing or is my best alternative to
> resort to a scriptlet?  
> (And for context, I actually want to do something along these lines in a
> tag file.)

You can do something like that but...

You'll need EL 3.0 which means Tomcat 8 minimum. I recommend 8.5.x in
preference to 8.0.x.

EL doesn't let you specify the package in the expression. You have to
explicitly import. With Tomcat, if you import the class into the JSP
page that automatically imports it into the EL expression. (Whether
Tomcat should do this or not is an open question with the EL Expert Group.)

Then factor in that EL only lets you reference static methods and fields
on concrete classes (which means you can't use Calendar since it is
abstract) and you get:

<%@ page language="java"
<%@ taglib prefix="c" uri="" %>
<!DOCTYPE html>
        <c:set var="X" value="${ GregorianCalendar.getInstance() }"/>
        <c:set var="Y" value="${ X.get(GregorianCalendar.YEAR) }"/>
        The year is ${ Y }

Drop the above test into a JSP page in the examples web application in
Tomcat 8 onwards and you'll get the output you are looking for.

Note I tested this with 9.0.x but 8.x should behave the same way.



To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message