tags:

views:

40

answers:

3

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.

+2  A: 

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
Your last statement is technically wrong. It's the other way round.
BalusC
@Balus, hmmm, To be honest, I use Asp.Net, not JSP. I hate being wrong...
jjnguy
Cool edit summary btw :)
BalusC
@Balus, thanks. Glad you appreciated it.
jjnguy
A: 

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()%>"/>
venJava
The `<%= x %>` does effectively the same as `<% out.print(x) %>`. So this answer is wrong.
BalusC
+3  A: 

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

<l:LoginStatus userId="${userId}" />
BalusC
Thankyou, that solved the problem noted and another problem with ClassCastExceptions that I was getting using the method I was trying.
Ankur
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