2

I am trying to pass a value from the session object to a custom tag <l:LoginStatus userId="<% out.print((String)session.getAttribute("userId")); %>"/>

Why does this line give me the error:
org.apache.jasper.JasperException: /index.jsp(1,1) /header.jsp(64,131) equal symbol expected

When I pass a hard coded value like this <l:LoginStatus userId="4"/>

Everything works fine.

It doesn't make any sense to me, I thought using out.print would make = uneccessary.

Ankur
  • 50,282
  • 110
  • 242
  • 312

3 Answers3

3

An alternative is to just use EL. That yields much cleaner code.

<l:LoginStatus userId="${userId}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thankyou, that solved the problem noted and another problem with ClassCastExceptions that I was getting using the method I was trying. – Ankur Oct 11 '10 at 15:02
  • CCE can occur when the object is *actually* not a `String` at all. After all, casting is unnecessary here. The `<%= %>` would implicitly call `String#valueOf()` on it. So `<%= session.getAttribute("userId") %>` would also just have worked. Using *scriptlets* is however [discouraged](http://stackoverflow.com/questions/3177733/howto-avoid-java-code-in-jsp-files). – BalusC Oct 11 '10 at 15:04
2

It should be:

<%= (String)session.getAttribute("userId") %>

In general it is much better practice to do things this way, instead of writing directly to a page. Besides, things don't work exactly as you seem to think they do.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
0

As you're printing the value of the an expression, you should make the statement as

<l:LoginStatus userId="<%=out.print((String)session.getAttribute("userId"))%>"/>

or

<l:LoginStatus userId="<%=out.print(session.getAttribute("userId")).toString()%>"/>
Venkat
  • 2,604
  • 6
  • 26
  • 36