Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.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 如何使JSP登录页上的会话无效_Java_Jsp_Session_Jsessionid - Fatal编程技术网

Java 如何使JSP登录页上的会话无效

Java 如何使JSP登录页上的会话无效,java,jsp,session,jsessionid,Java,Jsp,Session,Jsessionid,我有一个赤裸裸的登录JSP页面,在我第一次启动应用程序(访问)后,会立即创建一个会话,我觉得这是应用程序中的一个很大的安全漏洞,在我的web.xml代码中,我将我的设置为login.JSP,在我的loginServlet中,当在login.JSP中发布您的凭据并且所有内容都签出后,是什么时候会创建会话,老实说,我不明白为什么会创建会话,我希望能得到一些澄清 如果会话为空,我将使用servlet过滤器阻止访问,这就是我如何认识到在加载登录页面后创建了会话的原因 <welcome-file-l

我有一个赤裸裸的登录JSP页面,在我第一次启动应用程序(访问)后,会立即创建一个会话,我觉得这是应用程序中的一个很大的安全漏洞,在我的web.xml代码中,我将我的设置为login.JSP,在我的loginServlet中,当在login.JSP中发布您的凭据并且所有内容都签出后,是什么时候会创建会话,老实说,我不明白为什么会创建会话,我希望能得到一些澄清

如果会话为空,我将使用servlet过滤器阻止访问,这就是我如何认识到在加载登录页面后创建了会话的原因

<welcome-file-list>
   <welcome-file>login.jsp</welcome-file>
</welcome-file-list>

//from loginServlet, will only execute after posting from login page
            System.out.println("login successful");
            if (context != null){
                //get the old session and invalidate
                HttpSession oldSession = request.getSession(false);
                if (oldSession != null) {
                    System.out.println("Session is not null");
                    oldSession.invalidate();
                }

                //generate a new session & set to expiry in 5 mins
                HttpSession session = request.getSession(true);
                session.setMaxInactiveInterval(5*60);

login.jsp
//从loginServlet,将仅在从登录页面发布后执行
System.out.println(“登录成功”);
if(上下文!=null){
//获取旧会话并使其无效
HttpSession oldSession=request.getSession(false);
if(oldSession!=null){
System.out.println(“会话不为空”);
oldSession.invalidate();
}
//生成新会话并设置为5分钟后到期
HttpSession session=request.getSession(true);
session.setMaxInactiveInterval(5*60);

在JSP页面中,会话是自动创建的,在这个页面中也提出了相同的问题

在JSP中,一旦点击该JSP,就会创建一个会话(显然,除非它已经存在)

除非你使用指令

这也解释了您正在寻找的一些内容


<%session.invalidate();%>
<% HttpSession nsession = request.getSession(false);
if(nsession!=null) {
   String data=(String)session.getAttribute( "fname" );
   out.println(data);
}
else
  out.println("Session is not active");
%>
Output:
while opening display page, all the attributes are getting displayed on client (browser).
Since we have already called invalidate in the first line of errorpage.jsp, it is displaying the message “Session is not active” on the screen

Points to Note:
1) This will deactivate the session

<%session.invalidate();%>
2) This logic will exceute the if body when the session is active else it would run the else part.

<% HttpSession nsession = request.getSession(false);
if(nsession!=null)
   ...
else
   ...
%>
输出: 打开显示页面时,所有属性都将显示在客户端(浏览器)上。 由于我们已经在errorpage.jsp的第一行中调用了invalidate,它在屏幕上显示消息“Session is not active” 注意事项: 1) 这将停用会话 2) 当会话处于活动状态时,此逻辑将超越if主体,否则它将运行else部分。
在几乎所有情况下都会有一个会话(有时是每个请求的会话)。会话的存在是为了在客户端和服务器之间保持一种状态。通常会话是在容器中创建的,并注入到请求中,因此您的“request.getSession”(false);'将返回由容器创建的会话。要了解更多信息,请尝试通读:我很难理解为什么您认为创建会话应该是一个安全缺陷。会话只是您放入其中的任何内容的容器。它的唯一存在并不意味着用户可以在您的Web应用程序中做任何坏事,或者自动调用我已经登录了。