简体   繁体   English

如何在GAE / java中使用get / set cookie

[英]How to use get/set cookies in GAE/java

hi I'm trying to get all cookies of the browser, like I did in my java projects. 嗨,我正在尝试获取浏览器的所有cookie,就像我在java项目中所做的那样。

javax.servlet.http.Cookie    
String value = "";
    Cookie cookie;
    Cookie[] allcookies = request.getCookies();
        for(int i=0;i<allscookies.length;i++){
                cookie = allcookies[i];
                if(cookie.getDomain().equals("mydomain") && cookie.getName().equals("cookiename")){
                    value = cookie.getValue();
                }
            }

but does not work on my Google App Engine project and I get this error 但不适用于我的Google App Engine项目,我收到此错误

HTTP ERROR 500 HTTP错误500

Caused by: 引起:

java.lang.NullPointerException 显示java.lang.NullPointerException

Anyone knows any other way.Also try with this library but can not find how to use it no where com.google.appengine.repackaged.org.json.Cookie 任何人都知道任何其他方式。也尝试使用此库但无法找到如何使用它没有com.google.appengine.repackaged.org.json.Cookie

It's not about GAE. 这不是关于GAE。

Cookie[] allcookies = request.getCookies();

May be null in any environment - it depends on whether the browser sent any cookies for the URL you're calling. 在任何环境中都可以为null - 这取决于浏览器是否为您正在呼叫的URL发送了任何cookie。 Presumably your browser always had some cookies for the test URL you've used before deploying to GAE, and no cookies for the GAE URL. 据推测,您的浏览器总是为部署到GAE之前使用的测试URL提供了一些cookie,并且没有GAE URL的cookie。

Simply add an if (allcookies != null) { ... } around the loop. 只需在循环中添加if (allcookies != null) { ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM