我要做一个退出登陆的功能,因为是两个不同的页面(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"></i></a>
</div>
</header>