H_Franky 2019-12-17 22:56 采纳率: 20.8%
浏览 291
已采纳

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

我要做一个退出登陆的功能,因为是两个不同的页面(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 2019-12-18 19:42
    关注

    清除session,再重定向

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 宝塔面板无法添加Node项目,一直处于正在添加脚本页面
  • ¥50 Dkeil5 CT107D单片机的程序编写
  • ¥30 Ubuntu20.04中PVN3D复现过程交叉编译问题
  • ¥15 模拟电路求复阻抗和传递函数,请各位拍照写一下解答过程
  • ¥60 不懂得怎么运行下载来的代码
  • ¥15 CST导出3D模型图为什么和软件显示不一样?
  • ¥15 加热反应炉PLC控制系统设计(相关搜索:梯形图)
  • ¥15 python 用Dorc包报错,我的写法和网上教的是一样的但是它显示无效参数,是什么问题
  • ¥15 经过滑动平均后的一维信号还原用什么结构好呢?
  • ¥15 指定IP电脑的访问设置