2

I have the following JSP page:

<%@taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
test #1: value of PI is <c:out value="${java.lang.Math.PI}" />.
test #2: value of PI is ${java.lang.Math.PI}.
test #3: value of PI is <%= java.lang.Math.PI %>.

Somehow, only test #3 has output. why doesn't EL print out values of static variables?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
happymeal
  • 1,373
  • 14
  • 24
  • 3
    possible duplicate of [How to reference constants in EL?](http://stackoverflow.com/questions/3732608/how-to-reference-constants-in-el) – BalusC May 05 '11 at 14:49
  • 1
    @BalusC: You must have that one bookmarked by now :) – skaffman May 05 '11 at 14:49
  • 1
    @skaffman: I just enter "constants el" in Firefox browser address bar and copy the 1st link :) – BalusC May 05 '11 at 14:50

1 Answers1

5

For each of your examples, this is what is happening:

<c:out value="${java.lang.Math.PI}" />

This is looking for the variable or bean named java and trying to execute a method on it called lang. There is probably no variable or bean in your JSP page called Java so there is no output.

${java.lang.Math.PI}

This is the same as above, just written using EL only. It's the same in that it's looking for a variable or bean named java.

<%= java.lang.Math.PI %>

What this is doing is during the JSP compile, java.lang.Math.PI is being calculated and written into the JSP. If you look at the compiled JSP you will see the value written there.

The third example is evaluating the expression as if you were in a Java class. The first two examples expect 'java' to be a variable name.

Rachel
  • 3,691
  • 5
  • 32
  • 32