weixin_41359495
H_Franky
采纳率23.3%
2019-12-17 22:56

java web的Filter和cookie退出登陆问题求解!

5

我要做一个退出登陆的功能,因为是两个不同的页面(index和index1),但是用户表用的是同一个,即账号密码名称一样,现在从退出index页面能顺利推到login页面,但是从index1退出会先进入index页面后再次点击退出才退出到login,怎样才可以也让index1直接推到login?
下面是相关代码:

//LoginFilter部分
HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse res = (HttpServletResponse)response;
        String path = req.getContextPath();
        String basepath = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort()+path+"/";
        String uri = req.getRequestURI();
        if(uri.contains("/LoginServlet")||uri.contains("/RegisterServlet")||uri.contains("/css/")||uri.contains("/iconfont/")||uri.contains("/image/")||uri.contains("/js/")||uri.contains("/layui/")||uri.contains("/CheckCodeServlet")){
            chain.doFilter(request, response);
        }else if(uri.contains("/login.jsp")){
            Object user = req.getSession().getAttribute("user");
            if(user!=null){
                System.out.println("3");
                res.sendRedirect(basepath+"jsp\\index.jsp");
                //res.sendRedirect(basepath+"jsp\\index1.jsp");
            }else{
                chain.doFilter(request, response);
            }
        }else if(uri.contains("/register.jsp")){
            Object user = req.getSession().getAttribute("user");
            if(user!=null){
                res.sendRedirect(basepath+"jsp\\index.jsp");
            }else{
                chain.doFilter(request, response);
            }
        }
        else{
            Object user = req.getSession().getAttribute("user");
            if(user!=null){
                chain.doFilter(request, response);
            }else{
                res.sendRedirect(basepath+"login.jsp");
            }
        }

        //CookieLoginFilter部分
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        Cookie[] cookies = req.getCookies();
        boolean flag = false;
        boolean flag1 = false;
        if(cookies!=null){
            for(Cookie cookie:cookies){
                if("username".equals(cookie.getName())){
                    flag=true;
                    break;
                }
            }
        }
        if(flag){
            res.sendRedirect("LoginServlet");
        }else if(flag1) {
            res.sendRedirect("RegisterServlet");
        }else{
            chain.doFilter(request, response);
        }
        //LogoutServlet部分
        HttpSession session = request.getSession();
        /*session.removeAttribute("name");*/
        session.invalidate();
        Cookie cookieName = new Cookie("username",null);
        cookieName.setMaxAge(0);
        cookieName.setPath("/");
        Cookie cookiePwd = new Cookie("password",null);
        cookiePwd.setMaxAge(0);
        cookiePwd.setPath("/");
        Cookie cookieUpower = new Cookie("upower",null);
        cookieUpower.setMaxAge(0);
        cookieUpower.setPath("/");
        response.addCookie(cookieName);
        response.addCookie(cookiePwd);
        response.addCookie(cookieUpower);
        response.sendRedirect("login.jsp");

        //loginoutservlet部分
        HttpSession session = request.getSession();
        //session.removeAttribute("name");
        session.invalidate();
        Cookie cookieName = new Cookie("username",null);
        cookieName.setMaxAge(0);
        cookieName.setPath("/");
        Cookie cookiePwd = new Cookie("password",null);
        cookiePwd.setMaxAge(0);
        cookiePwd.setPath("/");
        Cookie cookieUpower = new Cookie("upower",null);
        cookieUpower.setMaxAge(0);
        cookieUpower.setPath("/");
        response.addCookie(cookieName);
        response.addCookie(cookiePwd);
        response.addCookie(cookieUpower);
        response.sendRedirect("login.jsp");

        //index1的jsp部分
        <header class="layout-header">
            <span class="header-logo">图书馆系统</span>
            <div class="dl-log">欢迎您,<span class="dl-log-user">${user.username}</span><a href="../login.jsp" title="退出系统" class="dl-log-quit">[退出]</a>
            <a class="header-menu-btn" href="javascript:;"><i class="icon-font">&#xe600;</i></a>
            </div>
        </header>


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • yushihai yushihai 2年前

    清除session,再重定向

    点赞 1 评论 复制链接分享
  • dashicaineng 蔡能教授,网站特聘专家 2年前
  • niaonao niaonao 2年前

    [退出]
    退出登录应该交互后端方法,不是跳转超链接,跳转超链接,前后端用户的session,cookie都还在,就是用户信息都未失效,对系统来说还是登录状态;

    请求后端方法,清除cookie,重定向到login.jsp

    点赞 1 评论 复制链接分享