java - 我设置了cookie的max age,但是cookie依然在关闭游览器后消失了
迷茫
迷茫 2017-04-18 10:51:59
0
3
1363

我设置了cookie的max age,但是cookie依然在关闭游览器后消失了。
我的controller:

package com.jiaotong114.jiaotong.controller;
@Controller
@RequestMapping("/")
public class CityIndex { 
@RequestMapping(value="/city/{cityName}", method = RequestMethod.GET)
public String printHello(ModelMap model, @PathVariable("cityName") String cityName, HttpServletRequest request, HttpServletResponse response) {

    Cookie[] c = request.getCookies();
    boolean isNew = true;
    for(int i = 0; i < c.length; i++) {
        if(c[i].getName().equals("cityName")) {
            c[i].setValue(cityName);
            c[i].setMaxAge(365 * 24 * 60 * 60);
            response.addCookie(c[i]);
            isNew = false;
        }
    }
    if(isNew) {
        Cookie cityNameCookie = new Cookie("cityName", cityName);
        cityNameCookie.setMaxAge(365 * 24 * 60 * 60);
        response.addCookie(cityNameCookie);
    }
    request.getSession().setAttribute("cityName", cityName);
    return "index";
}
}

访问http://localhost:8080/city/sh...调用这个controller,可以从图片看到cookie已经被成功添加到客户端了,时间是一年。

但是当我关闭了游览器,重新打开,访问http://localhost:8080,然后发现我的cookie不见了。见图2。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

répondre à tous(3)
伊谢尔伦

Le problème est résolu, cookie.setPath("/") est défini

阿神

C'est un problème de navigateur. . . . .

巴扎黑

cookie est déterminé par plusieurs attributs, je suppose que max-age n'en est qu'un.

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!