cookie不能在Internet explorer中工作

时间:2022-03-20 17:26:00

I am adding a value to cookie using

我正在为使用cookie添加一个值

Cookie testcookie = new Cookie ("test",test);
testcookie .setMaxAge(5*60);
response.addCookie(testcookie) ;

But I am not getting the cookie value in Internet explorer. code of getting cookie value

但是我在Internet explorer中没有得到cookie值。获取cookie值的代码

Cookie cookies [] = getRequest().getCookies ();
    Cookie myCookie = null;
    if (cookies != null)
    {

        for (int i = 0; i < cookies.length; i++) 
        {
            if (cookies [i].getName().equals ("test"))
            {
                myCookie = cookies[i];
                String testval=myCookie.getValue();
            }
        }
    }

But the same works in firefox,cooies are enabled in IE.How to resolve this ?

但是在firefox中也有同样的功能,在IE中也支持cooies。如何解决这个问题?

2 个解决方案

#1


1  

I faced the same problem these days, and I just found a solution. Try setting the cookie manually, as javax.servlet.http.Cookie does not allow you to set Expires attribute:

这些天我遇到了同样的问题,我找到了解决办法。尝试手动设置cookie,如javax.servlet.http。Cookie不允许您设置Expires属性:

StringBuilder cookie = new StringBuilder("test=" + test + "; ");

DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss 'GMT'", Locale.US);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 5*60);
cookie.append("Expires=" + df.format(cal.getTime()) + "; ");
cookie.append("Max-Age=" + (5*60));
response.setHeader("Set-Cookie", cookie.toString());

Hope it helps

希望它能帮助

#2


0  

The SimpleDateFormat solution works, although I noticed that the cookies weren't deleted at the time I expected. Seems that it printed the time in my local time, while the formatter presents it as GMT. If you set the calendar object to timezone GMT and use String.format, it will be formatted in the right timezone.

SimpleDateFormat解决方案是有效的,尽管我注意到cookie没有在我预期的时候被删除。好像是用我当地的时间打印出来的,而格式化程序显示的是GMT常量。如果将calendar对象设置为时区,并使用String。格式,它将被格式化在正确的时区。

// Your values here
String name = "test";
String value = "test";
String path = "/";
int maxAge = 60;


StringBuilder sb = new StringBuilder();
sb.append(name);
sb.append("=");
sb.append(value);

sb.append("; path=");
sb.append(path);

Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.add(Calendar.SECOND, maxAge);
sb.append("; Expires=");
sb.append(String.format(Locale.US, "%1$ta, %1$td-%1$tb-%1$tY %1$tH:%1$tM:%1$tS GMT", cal));
sb.append("; Max-Age=");
sb.append(maxAge);

response.setHeader("Set-Cookie", sb.toString());

#1


1  

I faced the same problem these days, and I just found a solution. Try setting the cookie manually, as javax.servlet.http.Cookie does not allow you to set Expires attribute:

这些天我遇到了同样的问题,我找到了解决办法。尝试手动设置cookie,如javax.servlet.http。Cookie不允许您设置Expires属性:

StringBuilder cookie = new StringBuilder("test=" + test + "; ");

DateFormat df = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss 'GMT'", Locale.US);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 5*60);
cookie.append("Expires=" + df.format(cal.getTime()) + "; ");
cookie.append("Max-Age=" + (5*60));
response.setHeader("Set-Cookie", cookie.toString());

Hope it helps

希望它能帮助

#2


0  

The SimpleDateFormat solution works, although I noticed that the cookies weren't deleted at the time I expected. Seems that it printed the time in my local time, while the formatter presents it as GMT. If you set the calendar object to timezone GMT and use String.format, it will be formatted in the right timezone.

SimpleDateFormat解决方案是有效的,尽管我注意到cookie没有在我预期的时候被删除。好像是用我当地的时间打印出来的,而格式化程序显示的是GMT常量。如果将calendar对象设置为时区,并使用String。格式,它将被格式化在正确的时区。

// Your values here
String name = "test";
String value = "test";
String path = "/";
int maxAge = 60;


StringBuilder sb = new StringBuilder();
sb.append(name);
sb.append("=");
sb.append(value);

sb.append("; path=");
sb.append(path);

Calendar cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT"));
cal.add(Calendar.SECOND, maxAge);
sb.append("; Expires=");
sb.append(String.format(Locale.US, "%1$ta, %1$td-%1$tb-%1$tY %1$tH:%1$tM:%1$tS GMT", cal));
sb.append("; Max-Age=");
sb.append(maxAge);

response.setHeader("Set-Cookie", sb.toString());