when I use <c:out value="${track}">
in a jsp, where should the attribute track
be located in (servletContext, httpSession and request)?
I tried to have a controller to set the attribute track
to the httpSession, but then ${track}
doesn't give me anything in the .jsp. On the other hand, if I set it to the servletContext, ${track}
give me the value. It doesn't seem right. Can you give a direction on passing attributes between .jsp (using jstl) and controllers (.java)? Thanks in advance.
It will under the hoods use JspContext#findAttribute()
to find the attribute. The linked javadoc mentions the following:
Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.
So, it will return the first non-null value which is found after searching in the order of page, request, session and application (servletcontext) scopes.
If you have attributes with the same name in multiple scopes and/or you'd like to get the attribute from a specific scope, then you can access it by the attribute maps available by the ${pageScope}
, ${requestScope}
, ${sessionScope}
and/or ${applicationScope}
. E.g.
${requestScope.track}
See also:
- Our EL wiki page
- How to access objects in EL expression language ${}
Back to your actual problem: if you have problems with accessing session scoped attributes, then it can only mean that the JSP is not using the same session as the servlet is using. You can debug it by printing the Session ID in servlet as follows
System.out.println(session.getId());
and in JSP by
${pageContext.session.id}
Both should print the same. If not, then it is definitely not sharing the same session. The session is domain, context and cookie dependent.
You can display all available session attributes by just printing ${sessionScope}
. It will display a string in format as described in AbstractMap#toString()
, containing all session attributes.
${sessionScope}