使用Cookie记录登录信息

以下为struts2的BaseAction里的处理方法。 
Java代码 
  1. public HttpServletResponse getResponse()  
  2.     {  
  3.         return ServletActionContext.getResponse();  
  4.     }  
  5.       
  6.     public HttpSession getSession(boolean flag)  
  7.     {  
  8.         return this.getRequest().getSession(flag);  
  9.     }  
  10.       
  11.     /** 
  12.      * 获取当前登录的会员信息 
  13.      * @return 
  14.      */  
  15.     public Member getLoginMember() throws Exception  
  16.     {  
  17.         Member member=null;  
  18.         String memberId=this.getCookieValue("memberId");  
  19.         String memberName=this.getCookieValue("memberName");  
  20.         String grade=this.getCookieValue("grade");  
  21.         String subject=this.getCookieValue("subject");  
  22.         if(memberId!=null&&memberName!=null&&grade!=null&&subject!=null)  
  23.         {  
  24.             member=new Member();  
  25.             member.setMemberId(Integer.valueOf(memberId));  
  26.             member.setMemberName(memberName);  
  27.             member.setGrade(Integer.valueOf(grade));  
  28.             member.setSubject(Integer.valueOf(subject));  
  29.         }  
  30.         return member;  
  31.     }  
  32.       
  33.     /** 
  34.      * 获取指定Cookie的值 
  35.      * @param cName 
  36.      * @return 
  37.      * @throws Exception 
  38.      */  
  39.     public String getCookieValue(String cName) throws Exception  
  40.     {  
  41.         String cValue=null;  
  42.         Cookie cookie=this.getCookie(cName);  
  43.         if(cookie!=null)  
  44.         {  
  45.             cValue=cookie.getValue();  
  46.         }  
  47.         return cValue;  
  48.     }  
  49.       
  50.     /** 
  51.      * 获取指定Cookie 
  52.      * @param cName 
  53.      * @return 
  54.      * @throws Exception 
  55.      */  
  56.     public Cookie getCookie(String cName) throws Exception  
  57.     {  
  58.         Cookie cookie=null;  
  59.         Cookie[] cs=this.getRequest().getCookies();  
  60.         if(cs!=null)  
  61.         {  
  62.             for(Cookie c:cs)  
  63.             {  
  64.                 String name=c.getName();  
  65.                 if(cName.equals(name))  
  66.                 {  
  67.                     cookie=c;  
  68.                 }  
  69.             }  
  70.         }  
  71.         return cookie;  
  72.     }  
  73.       
  74.     /** 
  75.      * 将设置cookie独立出来便于对cookie的配置统一控制 
  76.      * @param sName 
  77.      * @param sValue 
  78.      */  
  79.     public void addACookie(String cName,String cValue) throws Exception  
  80.     {  
  81.         Cookie cookie=new Cookie(cName,cValue);  
  82.         cookie.setMaxAge(365*24*60*60);  
  83.         this.getResponse().addCookie(cookie);  
  84.     }  
  85.       
  86.     /** 
  87.      * 将登录的用户信息存放到cookie中 
  88.      * @param member 
  89.      */  
  90.     public void setLoginMember(Member member) throws Exception  
  91.     {  
  92.         this.addACookie("memberId",member.getMemberId().toString());  
  93.         this.addACookie("memberName",member.getMemberName());  
  94.         this.addACookie("grade",member.getGrade().toString());  
  95.         this.addACookie("subject",member.getSubject().toString());  
  96.     }  
  97.       
  98.     /** 
  99.      * 删除所有Cookie 
  100.      * @throws Exception 
  101.      */  
  102.     public void invalidateCookies() throws Exception  
  103.     {  
  104.         Cookie[] cs=this.getRequest().getCookies();  
  105.         if(cs!=null)  
  106.         {  
  107.             for(Cookie c:cs)  
  108.             {  
  109.                 c.setMaxAge(0);  
  110.                 this.getResponse().addCookie(c);  
  111.             }  
  112.         }  
  113.     }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值