Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java httpsession验证(如果存在)_Java_Jsp_Servlets - Fatal编程技术网

Java httpsession验证(如果存在)

Java httpsession验证(如果存在),java,jsp,servlets,Java,Jsp,Servlets,strong文本我是新来的,需要一种正确的方式来验证。我跟着 5行代码。它没有httpsession,但仍将转到appointment.jsp。为什么会这样? 我跟着 它正在进行一次会议。org.apache.catalina.session。StandardSessionFacade@3b59e880但是用户没有登录到 是的。但我不知道为什么,怎么会这样 if (request.getSession(false) == null) { request.getServletContext

strong文本我是新来的,需要一种正确的方式来验证。我跟着

5行代码。它没有httpsession,但仍将转到appointment.jsp。为什么会这样? 我跟着

它正在进行一次会议。org.apache.catalina.session。StandardSessionFacade@3b59e880但是用户没有登录到

是的。但我不知道为什么,怎么会这样

if (request.getSession(false) == null) {
    request.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
} else if (request.getSession(false) != null) {
    request.getServletContext().getRequestDispatcher("/appointment.jsp").forward(request, response);
}

会话不是在用户登录后创建的,而是在浏览器首次请求容器时创建的。这使容器能够跟踪来自同一浏览器的后续请求。这通常使用具有唯一id(会话id)的cookie实现

所以,这取决于用户注销时发生了什么?您正在调用session.invalidate()。
我们不能仅仅因为会话对象不为null就说用户经过身份验证。

会话不是在用户登录后创建的,而是在浏览器向容器发出第一个请求时创建的。这使容器能够跟踪来自同一浏览器的后续请求。这通常使用具有唯一id(会话id)的cookie实现

所以,这取决于用户注销时发生了什么?您正在调用session.invalidate()。
我们不能仅仅因为会话对象不为空就说用户经过身份验证。

总会有一个HttpSession对象(好的,不总是,但大多数时候)-这不是经过身份验证的用户的指标

您需要设置会话属性,例如“authenticated”,以将此会话标记为已验证或未验证


您可以通过调用request.getSession().setAttribute(…)

来添加该对象(确定,不一定,但大多数情况下)-这不是经过身份验证的用户的指示符

您需要设置会话属性,例如“authenticated”,以将此会话标记为已验证或未验证


您可以通过调用request.getSession().setAttribute(…)

来添加此内容。默认情况下,JSP将创建一个会话。您可能不希望登录页面出现这种行为,因此请使用
login.jsp
中的page指令:

<%@ page session="false" %>


您还需要确保在成功登录之前访问的任何其他JSP不会创建会话。

默认情况下,JSP将创建会话。您可能不希望登录页面出现这种行为,因此请使用
login.jsp
中的page指令:

<%@ page session="false" %>
if (request.getSession(false).getAttribute("userLoggedIn") != null ) {
    if((Boolean)request.getSession(false).getAttribute("userLoggedIn") ) {
        request.getServletContext().getRequestDispatcher("/appointment.jsp").forward(request, response);
    }
} else {
    request.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
}


您还需要确保在成功登录之前访问的任何其他JSP都不会创建会话。

为什么。如果create为false并且请求没有有效的HttpSession,则此方法返回null。(这不是正确的方法吗?)HttpSession getSession()返回与此请求相关联的当前会话,或者如果请求没有会话,则创建一个会话。是的,请告诉我您的身份验证是否使用自定义逻辑(例如筛选器)完成?您的意思是即使我创建了会话,也要这样做。浏览器在用户打开页面时进行隐式会话?最好设置一个会话属性,如“会话存在”“是”/“否”是,最好在会话属性中设置一个“is_AUTHENTICATED”布尔标志。这样在登录时将其设置为true,如果未设置,则表示登录页面为false。另一个最好的方法是使用自定义逻辑在过滤器中跟踪会话,这将为您提供灵活性,如跟踪数据库中的每个会话或强制注销用户会话等。如果create为false并且请求没有有效的HttpSession,则此方法返回null。(这不是正确的方法吗?)HttpSession getSession()返回与此请求相关联的当前会话,或者如果请求没有会话,则创建一个会话。是的,请告诉我您的身份验证是否使用自定义逻辑(例如筛选器)完成?您的意思是即使我创建了会话,也要这样做。浏览器在用户打开页面时进行隐式会话?最好设置一个会话属性,如“会话存在”“是”/“否”是,最好在会话属性中设置一个“is_AUTHENTICATED”布尔标志。这样在登录时将其设置为true,如果未设置,则表示登录页面为false。另一个最好的方法是使用自定义逻辑在过滤器中跟踪会话,这将为您提供灵活性,例如跟踪数据库中的每个会话或强制注销用户会话etcif((布尔)request.getSession(false).getAttribute(“userLoggedIn”){是否正常?是,除了潜在的NPE。(getSesstion可以返回null,getAttribute也可以返回null,request也可以为null)是否可以?if((Boolean)request.getSession(false).getAttribute(“userLoggedIn”){out.println(“存在会话”);out.print(request.getSession(false));}else if(!(Boolean)request.getSession(false).getAttribute(“userLoggedIn”)| |(Boolean)request.getSession(false).getAttribute(“userLoggedIn”)==null{out.println(“没有会话”);out.print(request.getSession(false));嗯,这个样式不好。但是我认为你会实现你的计划,通过这个。(布尔)请求。getSession(false)。getAttribute(“userLoggedIn”)可以为null。Boolean loggedIn=(布尔)请求。getSession(false)。getAttribute(“userLoggedIn”);然后检查loggedIn是否为null。如果((布尔)请求。getSession(false)。getAttribute(“userLoggedIn”)){可以吗?可以,除了潜在的NPE。(getSesstion可以返回null,getAttribute也可以返回null,request也可以为null)这可以吗?如果((布尔)request.getSession(false)。getAttribute(“userLoggedIn”){out.println(“有会话”);out.print(request.getSession(false))}else if(!(布尔)request.getSession(false).getAttribute(“userLoggedIn”)| |(布尔)request.getSession(false).getAttribute(“userLoggedIn”)==null{out.println(“没有
if (request.getSession(false).getAttribute("userLoggedIn") != null ) {
    if((Boolean)request.getSession(false).getAttribute("userLoggedIn") ) {
        request.getServletContext().getRequestDispatcher("/appointment.jsp").forward(request, response);
    }
} else {
    request.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
}