java - 如何使用 JSTL 检查 session 中是否存在登录用户?

标签 java jsp redirect jstl servlet-filters

我想检查用户是否已使用 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");
   }
%>

最佳答案

试试这个:

<c:if test="${empty logged_in}">
<c:redirect url="index.jsp"></c:redirect>
</c:if>

关于java - 如何使用 JSTL 检查 session 中是否存在登录用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14764939/

相关文章:

java - 服务器程序不发送数据

java - 使用 Jbutton 停止线程 (Java)

java - Spring MVC 表单处理

java - 如何获取客户端 MAC 地址? (在jsp、java中)

.htaccess - 仅在主页上将 http 重定向到 https

java - HashMap 中的indexFor?

java - 写入行,读取行,然后关闭套接字

java - Java中获取 "eclipse-workspace"路径

django - 在 Django 中使用 HttpResponseRedirect 传递参数

javascript - 单击“确定”后显示警报消息并重定向