Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

jsp - which scope (application, servletContext, httpSession) will EL use for interpreting attributes

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


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}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...