如何使用 JSTL 检查会话中是不是存在已登录用户?
Posted
技术标签:
【中文标题】如何使用 JSTL 检查会话中是不是存在已登录用户?【英文标题】:How can I check for a presence of a logged in user in a session using JSTL?如何使用 JSTL 检查会话中是否存在已登录用户? 【发布时间】:2013-01-23 18:26:37 【问题描述】:我想检查用户是否使用 JSTL 登录。
我尝试了以下代码:
<c:if test="$sessionScope.logged_in == null">
<c:redirect url="index.jsp"></c:redirect>
</c:if>
我也试过下面的代码,但它抛出了一个IllegalStateException
:
<c:if test="$empty user">
<b>you are not logged in</b>
<c:redirect url="index.jsp"></c:redirect>
</c:if>
但它不起作用。
如何在 JSTL 中实现以下代码:
<%
String logged_in = (String) session.getAttribute("logged_in");
if (logged_in == null)
response.sendRedirect("index.jsp");
%>
【问题讨论】:
***.com/tags/servlet-filters/info @BalusC 建议的变体(参见他的链接中的示例)是更优选的解决方案。将 servlet 过滤器用于登录检查之类的事情是一种常见的做法。但当然这取决于你...... @informatik01 问题是关于 JSTL,而不是关于过滤器。 确实如此。 OP 使用了错误的工具来完成这项工作。 感谢您的帮助,我喜欢过滤器的概念:D 【参考方案1】:试试这个:
<c:if test="$empty logged_in">
<c:redirect url="index.jsp"></c:redirect>
</c:if>
【讨论】:
以上是关于如何使用 JSTL 检查会话中是不是存在已登录用户?的主要内容,如果未能解决你的问题,请参考以下文章