gpt4 book ai didi

java - 注销后, session 没有结束

转载 作者:行者123 更新时间:2023-11-30 22:50:41 25 4
gpt4 key购买 nike

我创建了一个 java 登录应用程序,其中一切正常,但是当我单击注销按钮时,它成功注销并重定向到 index.jsp 但在 index.jsp 页面中,如果我打印 session 值,那么它正在打印一样的,不知道为什么??但是,在注销时我已经终止了 session 。下面是代码,请指出可能的原因:

在 index.jsp 页面上,下面是检查 session 是否存在的代码。注销后它正在打印“isession 不为空”...

<%
if (session == null)
{
System.out.println("session is null");
session.removeAttribute("username");
session.removeAttribute("uniqueID");
session.invalidate();
}
else if(session != null)
{
System.out.println("isession is not null");
System.out.println(session);
}

%>

登录Servlet.java

String name = "";
JSONObject obj = result_array.getJSONObject(0);
String res = obj.get("result").toString();
HttpSession session = null;
if (res.equals("true")) {
try {
name = obj.get("name").toString();
session = request.getSession(true);
session.setAttribute("username", name);
session.setAttribute("uniqueID", uname);
//setting session to expiry in 15 mins
session.setMaxInactiveInterval(15*60);
Cookie userName = new Cookie("user", uname);
userName.setMaxAge(15*60);
response.addCookie(userName);

if("0".equals(obj.get("role").toString()))
{
session.setAttribute("role", "user");
response.sendRedirect("home.jsp");
}
else if("1".equals(obj.get("role").toString()))
{
session.setAttribute("role", "admin");
response.sendRedirect("AdminHome.jsp");
}
}
catch (JSONException ex)
{
System.out.println(getClass().getName()+" = " +ex.toString());
this.context.log(ex.toString());
}

logoutservlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
// Cookie[] cookies = request.getCookies();
// if (cookies != null) {
// for (Cookie cookie : cookies) {
// if (cookie.getName().equals("JSESSIONID")) {
// System.out.println("JSESSIONID=" + cookie.getValue());
// break;
// }
// }
// }
Cookie loginCookie = null;
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("user")) {
loginCookie = cookie;
break;
}
}
}
if (loginCookie != null) {
loginCookie.setMaxAge(0);
response.addCookie(loginCookie);
}
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(false);
if (session != null) {
session.removeAttribute("username");
session.removeAttribute("uniqueID");
session.removeAttribute("role");
session.invalidate();
}
out.print("You have Succefully logged out ");
response.sendRedirect("index.jsp");
out.flush();
out.close();
}
}

最佳答案

默认情况下,会自动为 JSP 创建一个 session ,当然除非它已经存在。因此,当您再次检查隐式 session 对象时,注销后,它是一个对象。

你可以通过打印来验证这一点

<%= session.isNew() %>

要为特定 JSP 关闭此功能,您需要设置 page 指令的 session 属性。

<%@ page session="false" %>

但这似乎没有必要,因为登录/注销状态始终可以由 session 属性的存在而不是 session 本身的无效性来确定。

关于java - 注销后, session 没有结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28250400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com