I think I'm missing something basic regarding Expression Language.
practice.jsp (below) outputs 14
, as expected.
<jsp:scriptlet>
request.setAttribute("a", 5);
request.setAttribute("b", 9);
</jsp:scriptlet>
${a+b}
practice2.jsp (below) outputs 0
.
<jsp:scriptlet>
Integer a = 5;
Integer b = 9;
</jsp:scriptlet>
${a+b}
What is going on in practice2.jsp? Why can't EL seem to evaluate these variables? Is this a scope issue, or am I missing something bigger?
The expression language construct
${a + b}
looks for attributes with keys a
and b
in the page, request, session, and servlet contexts, returning the first it finds. There is no way for it to read variables declared in scriptlets without explicitly adding them to any of those contexts with the key you would like to access them by.
I recommend you abandon scriptlets right away, for reasons expressed in this article and others.