views:

22

answers:

1

In the below code snippet, the intention is to take value for name from a variable, however no value gets output when it is tried with EL, while direct assignment works fine.

<%! String sName; %>
<%  sName="ABC"; %>
<H3> Hello <c:out value="${sName}"/> </H3>
<H3> Hello <c:out value="ABC"/> </H3>

What is missing or has gone wrong here?
Thanks in advance.

+2  A: 

When you write ${sName}, it doesn't mean that page looks for local variable with name sName. Local variables you have are immaterial.

You can set sName for later use with something like request.setAttribute("sName", "some value") from scriptlet. Or you can use jstl tag: <c:set var="sName" value="some value"/>.

There's also an option to use scriptlet to print value of your local variable, as you noted: <%= sName %>

Nikita Rybak