简体   繁体   中英

Trying to print a session variable in a jsp causes error “equals expected”

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.

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

<l:LoginStatus userId="${userId}" />

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.

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()%>"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM