10

I am trying string comparison with

<c:if test="${dept eq 'account'}"></c:if>

But this always returns false. I check the dept variable had the value 'account'. I also tried like this

<c:if test="${dept == 'account'}"></c:if> 

This also returns false.

But if I use the java code like this then it works fine

<%
if(dept.equals("account")){

blah blah blah
}

%>

Any help would be really appreciated.

Thanks

skaffman
  • 398,947
  • 96
  • 818
  • 769
user509755
  • 2,941
  • 10
  • 48
  • 82

1 Answers1

14

The symptoms indicate that you've declared it in the scriptlet scope, not in the EL scope. Scriptlets and EL doesn't share the same scope. EL uses under the covers PageContext#findAttribute() to resolve the variable. Put dept in one of the page, request, session or application scopes. Which one to choose depends on the sole purpose of dept itself. I'd start with the request scope. E.g. in a servlet:

request.setAttribute("dept", dept);

This way it'll be available in EL by ${dept}.

After all, best is to avoid using scriptlets completely. Java code belongs in Java classes, not in JSP files.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Excellent!! This is the perfect answer. Thanks a lot for your time. This is working fine. I think EL was not able to find the dept variable in any scope. As soon as I put it in request scope it starts working fine. – user509755 Nov 16 '10 at 18:13