4

I have a need to perform a bitwise test within a JSP but can't for the life of me figure out how to do it with EL.

I want to do something like:

<c:if test="${(test & testFor) == testFor}">
  <h3>Test Passed</h3>
</c:if>

Of course I can do it with ordinary JSP syntax:

<% if ((test & testFor) == testFor) { %>
  <h3>Test Passed</h3>
<% } %>
Brett Ryan
  • 26,937
  • 30
  • 128
  • 163
  • 2
    An EL function is indeed the way to go. You can find examples [here](http://stackoverflow.com/questions/3472699/jstl-fmt-library-throws-500-error-requires-sessions-enabled/3472823#3472823), [here](http://stackoverflow.com/questions/2752949/jsp-2-0-seo-friendly-links-encoding/2753495#2753495) and [here](http://stackoverflow.com/questions/2523430/hidden-features-of-jsp-servlet/2525995#2525995). – BalusC Aug 13 '10 at 12:47
  • Thankyou very much for your help BalusC, very much appreciated. – Brett Ryan Aug 13 '10 at 12:51

2 Answers2

5

I think bitwise operators are not implemented in JSTL (see here)

You can implement a JSTL function, called bitwiseAnd(int, int) and perform the bitwise test in Java code

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
3

JSP EL doesn't support bitwise operators (only arithmetic, logical and relational operators). If you want to do that, you'll need to encapsulate the operations within a java class, and expose that to the JSP. Alternatively, I believe you can write a custom function, that would look similar to those in the fn: JSTL namespace, but I'm not sure how to go about that.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • Thanks heaps, much appreciative, both your and Bozho's answers were very similar, but I had to choose one :) – Brett Ryan Aug 13 '10 at 12:51