分享

JAVA读取cookie的方法

 posondlq 2013-08-29

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

//-----------------------
public static  Cookie getCookie(HttpServletRequest request, String name) throws  Exception {

 Cookie[] cookies = request.getCookies();
 if(cookies!=null)
 {
 String username = "";
 String id = "";
 String level = "";
 String governid = "";
   DoradoContext ctx = DoradoContext.getContext(); 
 for (int i = 0; i < cookies.length; i++)
 {
 Cookie c = cookies[i];
 if(c.getName().equalsIgnoreCase("name"))
 {
 username = c.getValue();
 username =new String(username.getBytes("ISO8859_1"),"gbk");
 ctx.setAttribute(DoradoContext.SESSION, "name", username);
 System.out.println(username);
 }
   else if(c.getName().equalsIgnoreCase("id"))
   {
     id = c.getValue();
     ctx.setAttribute(DoradoContext.SESSION, "id", id);
      System.out.println(id);
   }
     else if(c.getName().equalsIgnoreCase("level"))
      {
        level = c.getValue();
        ctx.setAttribute(DoradoContext.SESSION, "level", level); 
      }
        else if(c.getName().equalsIgnoreCase("governid"))
         {
           governid = c.getValue();
           ctx.setAttribute(DoradoContext.SESSION, "governid", governid); 
           System.out.println(governid);
         }
 } //for
 } //if
return null;
 }
//-----------------------

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多