Java 注销后,会话不会结束

Java 注销后,会话不会结束,java,mysql,ajax,jsp,Java,Mysql,Ajax,Jsp,我已经创建了一个java登录应用程序,在该应用程序中一切正常,但当我单击“注销”按钮时,它成功注销并重定向到index.jsp,但在index.jsp页面中,如果我打印会话值,那么它将打印相同的值,我不知道为什么??但是,在注销时,我已终止会话。以下是代码,请说明可能的原因: 在index.jsp页面上,下面是检查会话是否存在的代码。注销后,它将打印“isession不为空” logoutservlet.java String name = ""; JSONObject

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

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

logoutservlet.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());
                }
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已经存在,否则会自动为JSP创建会话。因此,当您再次检查隐式
会话
对象时,注销后是一个新对象

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

<%= session.isNew() %>
这似乎是不必要的,因为登录/退出状态总是可以由会话属性的存在而不是会话本身的空值来确定

<%= session.isNew() %>
<%@ page session="false" %>