Java 在 JSP 中,如何使用 JSTL 检查请求中是否存在某些会话属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20597043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:01:47  来源:igfitidea点击:

In JSP, how can i check, using JSTL, if certain session attribute exists in request?

javajspservletsmodel-view-controllerjstl

提问by Vladimir

This is code in servlet:

这是servlet中的代码:

HttpSession session = request.getSession(true);
session.setAttribute("user", user);

I am forwarding request to JSP, where i want to check if there is session scoped user parameter attached.

我正在将请求转发到 JSP,我想在其中检查是否附加了会话范围的用户参数。

<c:if test="${??? - check if user is attached to request}">
/   /message
</c:if>

采纳答案by JB Nizet

<c:if test="${sessionScope.user != null}">
    There is a user **attribute** in the session
</c:if>

回答by TheDude

I think you mean checking the session scope right?

我认为您的意思是检查会话范围对吗?

<c:if test="${!empty sessionScope.user}">

回答by Nidhish Krishnan

you can do that by using the following code

您可以使用以下代码来做到这一点

Setting session in Servlet

设置会话 Servlet

HttpSession session = request.getSession(); 
session.setAttribute("user", user);

Access session value by ELin JSP

通过ELin访问会话值JSP

<p>${sessionScope:user}</p>

Checking the session in JSPusing JSTL

正在JSP使用中检查会话JSTL

<c:if test="${sessionScope:user != null}" > 
   session value present......
</c:if>