java操作cookie

2019-03-10 00:48|来源: 网络

java创建cookie

Cookie cookie1 = new Cookie("username", username);
cookie1.setMaxAge(30 * 24 * 60 * 60); //用户名保留1个月
cookie1.setPath("/");  //此时服务器上的所有页面都可以接收到该Cookie
response.addCookie(cookie1);

java获取cookie

Cookie[] cookies = request.getCookies();
if(cookies != null){
    for(int i = 0; i < cookies.length; i++){
        if(cookies[i].getName().equals("username")){
            username=URLDecoder.decode(cookies[i].getValue(), "utf-8");
    }
    }        
}

java删除cookie

Cookie[] cookies = request.getCookies();
    if(cookies != null){
        for(int i = 0; i < cookies.length; i++){
            Cookie temp = cookies[i];
            if(temp.getName().equals("username")){
                if("0".equals(b1)){
                    temp.setMaxAge(0);  //设置有效时间为0,则系统会自动删除过期的cookiessss
                    temp.setPath("/");     //项目所有目录均有效,这句很关键,否则不敢保证删除
                    response.addCookie(temp);  //重新写入,将覆盖之前的
                }
            }
        }
    }


总结: setPath设置Cookie适用的路径。如果不指定路径,Cookie将返回给当前页面(JSP页面或者Servlet的映射)所在目录及其子目录下的所有页面。

所以为什么很多时候你认为已经删除了cookie,但是在另一个页面cookie就是存在,所以一定要加上:这两段代码:

temp.setPath("/");      //项目所有目录均有效,这句很关键,否则不敢保证删除

response.addCookie(temp);   //重新写入,将覆盖之前的


转自网络

相关问答

更多
  • 这个很简单啊,如果你有HttpServletRequest对象的引用,调用 Cookie[] cookies = request.getCookies();就可以获取到cookie信息。
  • 您正在使用错误的方法读取Cookie。 Cookie[] cookies = request.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("cookieName")) { //do something //value can be retrieved using #cookie.getValue() } } } 用这 ...
  • -1的MaxAge表示您希望Cookie在会话期间持续存在。 您想要将MaxAge设置为0。 从API文档 : 负值表示Cookie不会被持久存储,并在Web浏览器退出时被删除。 零值会导致Cookie被删除。 The MaxAge of -1 signals that you want the cookie to persist for the duration of the session. You want to set MaxAge to 0 instead. From the API docume ...
  • 我一直收到来自服务器的错误,说我的客户端不支持cookie,我应该打开它们。 以下行停止了这些错误。 希望这对你有用。 httpClient.setCookieStore(new BasicCookieStore()); I kept getting errors back from the server saying my client didn't support cookies and I should turn them on. The following line stopped those e ...
  • 当你说“在我的网站”时,这是否意味着你的网站部署在不同的(子)域? 默认情况下,Cookie只对其设置的主机可见,这意味着从“www.example.com”设置的cookie对“other.example.com”不可见。 你可以通过明确指定cookie域为两者共同(例如“example.com”)来解决这个问题。 Actually, it turned out to be a path issue. I know I didn't paste any code, but I was creating t ...
  • 你不能在本地主机上设置cookie,至少不能在Chrome上设置。 您至少需要两个部分名称,例如mytestsite.local,或者您需要设置 Domain = null 在C#代码中。 You can't set cookies on localhost, at least not with Chrome. You need at least a two part name, e.g. mytestsite.local, or you need to set Domain = null in the ...
  • 默认情况下Cookie是每个域的。 通常情况下,负载均衡器将拥有公共URL,并且Web服务器将位于服务器的静态内容之后。 像Java这样的应用程序服务器可以直接位于负载均衡器之后,也可以通过Web服务器。 因此,只要所有部署的应用程序都托管在同一个域中,浏览器就会默认将所有cookie发送到所有应用程序。 所以是的,在你的情况下,它会工作。 Cookies by default are per domain. Normally, load balancer will be having the public ...
  • 你可以通过$_COOKIE查看。 if(!isset($_COOKIE['loggedIn'])){ header('Location: /path/to/another/page'); exit; } 您可以在单独的文件中对其进行编码,并将其包含在每个页面中,或者您可以在XHR中实现它。 You can check it via $_COOKIE. if(!isset($_COOKIE['loggedIn'])){ header('Location: /path/to/another/pag ...
  • URL myUrl = new URL("http://www.hccp.org/cookieTest.jsp"); URLConnection urlConn = myUrl.openConnection(); urlConn.connect(); 由于服务器可以在单个请求中设置多个cookie,因此我们需要遍历响应头,查找名为“Set-Cookie”的所有头。 String headerName=null; for (int i=1; (headerName = uc.getHeaderFieldKe ...
  • 首先,您需要在settings.py文件中添加开放cookie COOKIES_ENABLED = True 这是我的测试蜘蛛代码供您参考。 我测试了它并通过了 from scrapy.contrib.spiders import CrawlSpider, Rule from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor from scrapy.http import Request from scrapy import log ...