H_Franky 2019-12-17 22:56 采纳率: 22.7%
浏览 290
已采纳

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条)

报告相同问题?

悬赏问题

  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页