日期:2014-05-17  浏览次数:21060 次

为什么设置了cookie却获取不到值
我要做的是 在一个页面 设置 cookie值,然后该页面的 iframe jsp页面再去拿值,为什么拿到的不是我设置进去的值,而是cookie本身的ID?
HTML code
ProductDAO pd = new ProductDAO();

    String cookieName = "product";

    String cookieValue = "";

    Cookie[] cookies = request.getCookies();

    Cookie myCookie = null;

    if (null != cookies) {

        for (int k = 0; k < cookies.length; k++) {

            if (cookies[k].getName().equals(cookieName)) {

                myCookie = cookies[k];

                break;

            }

        }

    }

    if (myCookie != null) {

        cookieValue = myCookie.getValue();

        String[] aryStr = cookieValue.split(",");

        boolean aryFlag = false;

        for (int k = 0; k < aryStr.length; k++) {

            if (aryStr[k].equals(pid)) {

                aryFlag = true;

            }

        }

        if (aryFlag == false) {

            cookieValue += pid + ",";

            Cookie cookie1 = new Cookie(cookieName, cookieValue);

            cookie1.setMaxAge(365 * 24 * 3600);
            cookie1.setPath("/");
            response.addCookie(cookie1);

        }

    } else {

        cookieValue = "," + pid + ",";

        Cookie cookie1 = new Cookie(cookieName, cookieValue);
        cookie1.setPath("/");
        cookie1.setMaxAge(365 * 24 * 3600);
        response.addCookie(cookie1);
    }
这里面设置

这里面去获取
HTML code
DoText dt = new DoText(); 
  ProductDAO pd = new ProductDAO();
  String cookieName = "product";
    String cookieValue = "";
    Cookie[] cookies = request.getCookies();
    Cookie myCookie = null;
    if(null != cookies){
    for(int k=0;k<cookies.length;k++){
        if(cookies[k].getName().equals(cookieName)){
            myCookie = cookies[k];
            cookieValue = myCookie.getValue();
            out.print(cookieValue);
            break;
            }
        }
    }

为什么拿不到 我设置进去的value

------解决方案--------------------
探讨

myCookie = cookies[k];
cookieValue = myCookie.getValue();
out.print(cookieValue);
改成这样
myCookie = cookies[k];
String ck=cookies[k].getValues();
out.print(cookieValue);