2

Here's some code

<% String what = (String)session.getAttribute("BANG"); %>
<c:set var="bang" value="Song" />

I'm getting a string out of the session and I want to compare it to a string in a jstl variable.

I have tried an if in

<% if() { %> some text <% } %> 

also tried

<c:if test="${va1 == va2}" </c:if>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user931501
  • 91
  • 3
  • 12
  • I am not clear as to what you want to accomplish. Your first line at the top creates a scripting variable and assigns it a value from the session-scoped variable. Your second line sets a value for the page-scoped variable. What condition are you trying to test in the third and fourth lines? – rickz Dec 04 '12 at 05:50
  • Please don't mix "scriptlets" with "javascript". Those are absolutely not the same. I fixed the (initially very confusing) question title. – BalusC Dec 04 '12 at 14:48

1 Answers1

2

For starters, it's recommended to stop using scriptlets, those <% %> things. They don't work nicely together with JSTL/EL. You should choose for the one or the other. As scriptlets are officially discouraged since a decade, it makes sense to stop using them.


Coming back to your concrete question, the following scriptlet

<% String what = (String)session.getAttribute("BANG"); %>

can just be done in EL as follows

${BANG}

So, this should do it for you:

<c:if test="${BANG == 'Song'}">
    This block will be executed when the session attribute with the name "BANG"
    has the value "Song".
</c:if>

Or, if you really need to have "Song" in a variable, then so:

<c:set var="bang" value="Song" />
<c:if test="${BANG == bang}">
    This block will be executed when the session attribute with the name "BANG"
    has the same value as the page attribute with the name "bang".
</c:if>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555