日期:2014-05-20  浏览次数:20805 次

COOKIE问题 在线求助啊 急死人了
登陆action中判断数据库中密码是否相同 相同就把用户名放入COOKIE里 后面另一个action 代码如下


Java code

HttpServletResponse rep = ServletActionContext.getResponse()  ;
 Cookie nameCookie = new Cookie("loginname",LoginName);   
                nameCookie.setMaxAge(60);   
               
                rep.addCookie(nameCookie);  






Java code

HttpServletRequest req=ServletActionContext.getRequest();
 Cookie[] cks = req.getCookies();
           String loginname="";
         for(Cookie ck :cks)
         {
             if("loginname".equals(ck.getName())){
                 loginname=ck.getValue();     
             }
              System.out.println(ck.getValue());
              System.out.println(ck.getName());
         
         }
            
         System.out.println(cks.length);
         
           System.out.println("$$$$$$$$$$$ cookie-- name:" + loginname );   
          




输出如下
9D65E9557CE5C1D12C1EA264DF418B53
JSESSIONID
1
$$$$$$$$$$$ cookie-- name:



为什么取不到?。还是就没存进去?哪里出错了






------解决方案--------------------
Java code



import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

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

public class CookieUtil {
    /**
     * 通过cookie name获取一组cookie
     * @param request
     * @param name
     * @return
     */
    public static Cookie getCookie(HttpServletRequest request, String name) {
        Cookie cookies[] = request.getCookies();
        if (cookies == null || name == null || name.length() == 0)
            return null;
        for (int i = 0; i < cookies.length; i++) {
            if (name.equals(cookies[i].getName())) { 
                //    && request.getServerName().equals(cookies[i].getDomain())) {
                return cookies[i];
            }
        }
        return null;
    }
    
    /**
     * 通过cookie name 读取 value
     * @param request
     * @param name
     * @return
     */
    public static String readCookie(HttpServletRequest request, String name) {
        String value = null;
        try {
            Cookie cookies[] = request.getCookies();
            if (cookies == null || name == null || name.length() == 0)
                return null;
            for (int i = 0; i < cookies.length; i++) {
                if (name.equals(cookies[i].getName())) {
                    // && request.getServerName().equals(cookies[i].getDomain())) {
                    value = URLDecoder.decode(cookies[i].getValue(), "UTF-8");
                    return value;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 删除cookie
     * @param request
     * @param response
     * @param cookie
     */
    public static void deleteCookie(HttpServletRequest request,
            HttpServletResponse response, Cookie cookie) {
        if (cookie != null) {
            cookie.setPath(getPath(request));
            cookie.setValue("");
            cookie.setMaxAge(0);
            response.addCookie(cookie);
        }
    }
    /**
     * 通过cookie name 删除一组cookie
     * @param request
     * @param response
     * @param name
     */
    public static void deleteCookie(HttpServletRequest request,
            HttpServletResponse response, String name) {
        deleteCookie(request, response, getCookie(request, name));
    }
    
    /**
     * 设置新的cookie ,默认时间是0x278d00(一个月)
     * @param request
     * @param response
     * @param name
     * @param value
     */
    public static void setCookie(HttpServletRequest request,
            HttpServletResponse response, String name, String value) {
        setCookie(request, response, name, value, 0x278d00);
    }
    /**
     * 设置新的cookie,maxAge为默认值,cookie仅仅保存在浏览器进程中
     * @param request
     * @param response
     * @param name
     * @param value
     * @param maxAge
     */
    public static void setCookie(HttpServletRequest request,
            HttpServletResponse response, String name, String value, int maxAge) {
        Cookie cookie = new Cookie(name, value == null ? "" : value);
//        cookie.setMaxAge(maxAge);            //设置cookie生命周期,单位秒(365*24*60*60 一年),如果设置为负值的话,则为浏览器进程Cookie(内存中保存),关闭浏览器就失效。默认为-1
        cookie.setPath(getPath(request));    //-设置Cookie适用的路径。如果不指定路径,Cookie将返回给当前页面所在目录及其子目录下的所有页面。
                                            //-这里的方法可以用来设定一些更一般的条件。例如,setPath("/"),此时服务器上的所有页面都可以接收到该Cookie。
        response.addCookie(cookie);
    }
    /**
     * 私有方法
     * 获取项目路径,如/nc_System
     * @param request
     * @return
     */
    private static String getPath(HttpServletRequest request) {
        String path = request.getContextPath();
        return (path == null || path.length() == 0) ? "/" : path;
    }
}