首页新闻找找看学习计划

学习jsp过程中cookie删除不了

0
悬赏园豆:5 [已关闭问题] 关闭于 2016-05-21 12:53
 1 package com.bx.course;
 2 import java.io.IOException;   
 3 
 4 import javax.servlet.ServletException;   
 5 import javax.servlet.http.Cookie;   
 6 import javax.servlet.http.HttpServlet;   
 7 import javax.servlet.http.HttpServletRequest;   
 8 import javax.servlet.http.HttpServletResponse;   
 9 public class LoginServlet extends HttpServlet {   
10     public void doGet(HttpServletRequest request, HttpServletResponse response)   
11             throws ServletException, IOException {   
12         this.doPost(request, response);   
13     }   
14     public void doPost(HttpServletRequest request, HttpServletResponse response)   
15                throws ServletException, IOException {   
16         String username=request.getParameter("username");   
17         String password=request.getParameter("password");   
18         String savetime=request.getParameter("saveTime"); 
19         System.out.println("直接登录usrename  "+username+"  password "+password);
20         if(CheckLogin.login(username, password)){   
21             if(null!=savetime){   
22                 int saveTime=Integer.parseInt(savetime);//这里接受的表单值为天来计算的   
23                 int seconds=saveTime*24*60*60;   
24                 Cookie cookie = new Cookie("abc", username+"=="+password); 
25                 cookie.setMaxAge(666666);  
26                 cookie.setPath("/");
27                // cookie.setDomain("localhost:8080/TestSystem");
28                 response.addCookie(cookie);   
29             }   
30             request.getSession().setAttribute("username",username);   
31             request.getRequestDispatcher("/Student.jsp").forward(request,response);   
32         }else{   
33             request.getRequestDispatcher("/failure.jsp").forward(request,response);   
34         }   
35  }   
36 }   
package com.bx.course;

/**
 * Filter可以实现对请求的过滤和重定向等,也就是说可以操作request和response,session等对象,listner只能监听到以上对象的属性的修改。
 */

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class IndexFilter implements Filter {
    
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        
             System.out.println("检查cookie");
            HttpServletRequest request = (HttpServletRequest) arg0;   
            HttpServletResponse response = (HttpServletResponse) arg1;   
            Cookie[] cookies = request.getCookies();  
            String[] cooks = null;   
            String username = null;   
            String password = null; 
            if (cookies != null) {   
                for (Cookie coo : cookies) {   
                    String aa = coo.getValue(); 
                    if(aa.indexOf("==")!=-1){
                        cooks = aa.split("==");   
                        if (cooks.length == 2) {
                            System.out.println("找到了cookie "+cooks[0]+cooks[1]);
                            username = cooks[0];   
                            password = cooks[1];   
                        }   
                    }                
                }   
            } 
            else{
                
            }
            if (CheckLogin.login(username, password)) {
                System.out.println("检查cookie成功,登录");
                request.getSession().setAttribute("username",username);   
                request.getRequestDispatcher("/Student.jsp").forward(request, response);
            }else{   
                arg2.doFilter(request,response );   
            }   
        }   
        public void init(FilterConfig arg0) throws ServletException {   
            // TODO Auto-generated method stub   
        }
        @Override
        public void destroy() {
            // TODO 自动生成的方法存根
            
        }   
}
 1 <%
 2         response.setHeader("refresh", "30;url=index.jsp");//定时跳转
 3         response.setHeader("Pragma", "No-cache");
 4         response.setHeader("Cache-Control", "no-cache");
 5         response.setDateHeader("Expires", 0);
 6         response.flushBuffer();
 7 
 8         /*Cookie[] cookies = request.getCookies();
 9          if(cookies != null){
10          for (Cookie coo : cookies){
11          if(coo.getName().equals("user")){
12          Cookie cookie = new Cookie("user",null);
13          coo.setMaxAge(0);
14          coo.setPath("/TestSystem");
15          coo.setDomain("localhost");
16          response.addCookie(coo);
17          System.out.println("执行到我了  cookie.getName()"+coo.getName());
18          System.out.println("执行到我了  cookie.getValue()"+coo.getValue());
19          }
20         
21          }
22          } */
23         Cookie[] cookies = request.getCookies();
24         if (cookies != null) {
25             for (Cookie coo : cookies) {
26                 if (coo.getName().equals("abc")) {
27 
28                     System.out.println("0.执行到删除cookie的Value"
29                             + coo.getValue());
30                     Cookie cookie = new Cookie("abc", null);
31                     System.out.println("0.执行到删除cookie ");
32                     cookie.setMaxAge(0);
33                     cookie.setPath("/");
34                     response.addCookie(cookie);
35                     System.out.println("删除后的cookie的Value" + coo.getValue());
36 
37                     System.out.println("1.执行到删除cookie的Value"
38                             + coo.getValue());
39                     Cookie cookie0 = new Cookie(coo.getValue(), null);
40                     System.out.println("1.执行到删除cookie0 ");
41                     cookie0.setMaxAge(0);
42                     cookie0.setPath("/");
43                     response.addCookie(cookie0);
44                     System.out
45                             .println("删除后的cookie0的Value" + coo.getValue());
46 
47                     System.out.println("2.执行到删除cookie ");
48                     coo.setValue("");
49                     coo.setMaxAge(0);
50                     coo.setValue("");
51                     coo.setPath("/");
52                     response.addCookie(coo);
53                     System.out.println("2.删除后的coo的Value" + coo.getValue());
54 
55                 }
56 
57             }
58         }
59         Cookie[] cookies6 = request.getCookies();
60         for (Cookie cookie1 : cookies6) {
61             cookie1 = new Cookie(cookie1.getName(), null);
62             cookie1.setMaxAge(0);
63             cookie1.setPath("/");
64             response.addCookie(cookie1);
65         }
66 
67         response.setHeader("Set-Cookie", "abc=000");
68 
69         /*  System.out.println("执行到我了  cookie.getName()"+coo.getName());
70          System.out.println("执行到我了  cookie.getValue()"+coo.getValue()); */
71 
72         //request.getSession().setAttribute("username", null);//退出登陆应该将user对象从session域中移除
73         request.getSession().invalidate();//注销  销毁session
74         //session.invalidate();    
75         //session.removeAttribute("username");
76     %>

 cookie删除部分有点乱,但都是想删除了

小豫的主页 小豫 | 初学一级 | 园豆:170
提问于:2016-05-14 17:49
< >
分享
所有回答(1)
0
小光 | 园豆:1830 (小虾三级) | 2016-05-16 09:52

ha是不行

支持(0) 反对(0) 小豫 | 园豆:170 (初学一级) | 2016-05-21 11:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册