Session 在invalidate()之后,是否可以从请求对象获取新会话?

Session 在invalidate()之后,是否可以从请求对象获取新会话?,session,servlets,invalidation,Session,Servlets,Invalidation,会话失效后,是否可以通过request.getSession()通过请求对象获取新会话,而无需发出新请求 我的请求对象流到第1页到第2页,第2页到第1页,第1页到第2页,第2页到第2页。请求页不能更改,但每次请求第1页到第2页时,我们都需要获取会话的详细信息并使其无效,然后再次创建。。 像这样 HttpSession session = request.getsession(false) String user = (String)session.getAttribute("name"); se

会话失效后,是否可以通过request.getSession()通过请求对象获取新会话,而无需发出新请求

我的请求对象流到第1页到第2页,第2页到第1页,第1页到第2页,第2页到第2页。请求页不能更改,但每次请求第1页到第2页时,我们都需要获取会话的详细信息并使其无效,然后再次创建。。 像这样

HttpSession session = request.getsession(false)
String user = (String)session.getAttribute("name");
session.invalidate();
session=request.getSession(true);
RequestDispacher dis = request.requestDispatcher("path");
dis.forword(request,respone);
Cookie[] c = request.getCookies();
            for(Cookie k: c){
                if(k.getName().equalsIgnoreCase("JSESSIONID")){
                    System.out.println("k.getValue() : "+k.getValue());
                    System.out.println("httpSession.getId() : "+httpSession.getId());
                    k.setValue(httpSession.getId());
                }
            }
但第二次不起作用 它提供空会话或详细信息

还可以尝试在即将到来的cookie中设置会话id 像这样

HttpSession session = request.getsession(false)
String user = (String)session.getAttribute("name");
session.invalidate();
session=request.getSession(true);
RequestDispacher dis = request.requestDispatcher("path");
dis.forword(request,respone);
Cookie[] c = request.getCookies();
            for(Cookie k: c){
                if(k.getName().equalsIgnoreCase("JSESSIONID")){
                    System.out.println("k.getValue() : "+k.getValue());
                    System.out.println("httpSession.getId() : "+httpSession.getId());
                    k.setValue(httpSession.getId());
                }
            }
invalidate()之后

你应该写

然后
getSession()
方法将检查会话是否已经存在。如果
会话
存在,它将
返回该会话对象,否则将创建一个新会话并返回给客户端

否则,如果您尝试执行以下操作

session.inValidate();
session..trySomething()  //leads to exception "Session already invalidated."
根据,只需调用
request.getSession()

返回与此请求关联的当前HttpSession,或者,如果没有当前会话且create为true,则返回新会话

如果create为false且请求没有有效的HttpSession,则此 方法返回null

要确保会话得到正确维护,必须调用此 方法,然后提交响应。如果容器正在使用 Cookie用于维护会话完整性,并被要求创建新的 会话提交响应时,将出现IllegalStateException 扔

因此,调用
getSession
方法将创建一个新会话:

final HttpSession session = request.getSession()
下面是一个JSP示例,它证明了代码的有效性:

test.jsp

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Session invalidation test</title>
</head>
<body>
<% 

// Uses implicit session for JSP

out.println("Session is " + session);
session.invalidate();
out.println("\nNew session is " + request.getSession());

request.getRequestDispatcher("/test2.jsp").forward(request, response);

%>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Session invalidation test</title>
</head>
<body>
<% 

out.println("Session is " + request.getSession());

%>
<h1>Test 2</h1>
</body>
</html>

这表示执行了
test.jsp
并成功地转发到test2.jsp。

如果需要,为什么要使会话无效?出于安全目的,我尝试这样做,但下次我们在同一请求对象上获取会话对象。它给出null…+1来解释如何通过尝试调用invalidated上的方法引发异常一场谢谢。您也可以使用
request.getSession()
而不使用布尔参数。我同意您的ans,但我的问题是在这之后。println(“会话是”+Session);session.invalidate();println(“新会话是”+request.getSession());调用request dispatcher和forword到另一个路径,调用session request.getSession,这样我就可以得到它。它提供null,但我需要在我的项目中维护会话,并向任何人显示会话。我认为您需要认真评估您的需求,真正理解安全意味着什么,您如何使用JavaEE实现它,也许可以咨询安全web应用程序方面的专家。看看OWASP,首先理解它。查看我的更新答案;我扩展了我的测试来向你证明什么是有效的。