hi I'm trying to get all cookies of the browser, like I did in my java projects.
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
HTTP ERROR 500
Caused by:
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
It's not about 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. 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.
Simply add an if (allcookies != null) { ... } around the loop.
Related
I have Servlet web application running at subdomain can access other cookies but other cookies set by sso application into parent domain level is not access. But I can see those cookies are there in the browser.
In the servlet application, I have the code to read cookies:
httpRequest = (HttpServletRequest)request;
Cookie[] cookies = httpRequest.getCookies();
if(cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("SESSION_ID")) accessToken = cookie.getValue();
}
}
The above code only can retrive the cookies set by subdomain application. If I checked at browser cookies list I can see the "SESSION_ID" cookie and it's value with primary domain.
Cookies looks like this:
Name Value Domain
SESSION_ID 123 .primarydomain.com
XYZ 001 subdomain.primarydomain.com
ABC 2233
In the above cookies list I can only access XYZ and ABC but not the value of SESSION_ID. Am I missing some basic thing here?
I want to read parent domain(domain.com) cookies
Running the code from xyz.domain.com
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
//only subdomain cookies(i.e. xyz.domain.com) are displayed
}
}
Here I am not getting domain.com cookies
Similar to the below link
Access parent domain cookies from an application running on a subdomain
Update:
Cannot edit the cookies .i.e. cannot set domain of the cookie
When cookie is created in domain.com, you need to set domain as cookie.setDomain("."), Then cookie will be available in sub domain.
I am using pdk jdeveloper portlet.
I have deployed ear on tomcat server with host www.test1.com:8080/
now i m using it's provider on oracle server with same host but different port like www.test1.com:9090/
so here i m not able to use cookie or session thing on www.test1.com:9090/
I have set cookie using javascript as below:
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
setCookie("USER","test",365);
now tried to get this cookie on portlet page as below.
PortletRenderRequest pReq = (PortletRenderRequest)
request.getAttribute(HttpCommonConstants.PORTLET_RENDER_REQUEST);
try 1: Cookie[] cookies =request.getCookies(); // get null
try 2: Cookie[] cookies = (Cookie[])pReq.getCookies(); // get null
how can i achieve this ? please help.
You have to set a domain on cookie. link, This gives you a good start. I read this article and found that its easy to share cookie across sub domain but complicated to share it across different domain. This would also be useful link
I am having a problem of setting the data of a (persistent/cross browser session) cookie correctly inside a Servlet and the reading it in a Filter.
the code of the Servlet (running at log-in time) is:
String encodedValue = new String(Base64
.encodeBase64(req.getParameter("account").getBytes()));
Cookie cookie = new Cookie("projectAuthenticationCookie", encodedValue );
cookie.setMaxAge(24*60*60);
cookie.setPath("/");
res.addCookie(cookie);
This will get the cookie inside the response, but the when I read it within my filter with the following code:
Cookie authenticationCookie = null;
Cookie[] cookies = ((HttpServletRequest) request).getCookies();
for (Cookie cookie : cookies){
if ("projectAuthenticationCookie".equals(cookie.getName())) {
authenticationCookie = cookie;
}
}
I only get the value I set right, all other fields are either null, empty or different. Max age for example always returns -1 and thus the cookie will never persist.
I tried setting the expires-header with:
res.setDateHeader("Expires", System.currentTimeMillis() + 24*60*60*1000);
as I read that without a valid expires-header the session will timeout anyway (correct me if I am wrong), but that didn't help either...
One issue I am thinking of is that I am running on localhost (tried setting cookie.setDomain("localhost") but also no luck). My web server/serclet container is Jetty 7 but I do not think that this is relevant...
Any hints?
The fields other than name and value are not populated (and thus not meaningful) on cookies you get from a request.
These fields are intended to inform the browser about the max age; path, etc. of the cookie, but the browser doesn't send back this information to the server. The only time where it's important to have the correct max age, path, etc. is when you create a cookie and add it to the response. Use your browser to check if it stores the correct information instead of trying to find it at server-side.
When I retrieve the cookies in my java servlet, all of the values from getPath() are null.
So if a cookie with the same name is set in directory /foo, and at the root directory, I retrieve two cookies with the same exact name, but I can't differentiate them because getPath() returns null for both.
I looked in firebug and saw that firefox was not sending anything for the path.
My application uses a "rememberme" cookie with the path set to "/". Everything works fine as long as there is only one cookie with name rememberme. But if somehow another cookie gets set with the same name on a different path like /foo, then my application won't know which one is the one I set for the root.
How can I differentiate the cookies? Do I need to worry about a cookie existing with the same name in a subdir, or can I just assume there will be only the one I set?
If the browser doesn't send a path, you should set the path to "/" in your Cookie handler.
Your server sets the cookies, not the web browser, so if you set all the paths for the cookies that you create to "/" for the same domain, you don't have to worry about it.
I'm not sure how much this will help you but I recently wrote this method to retrieve cookies from a URLConnection object and return them as a string:
public String getCookies(URLConnection connection) {
String headerName = null;
String cookie = "";
for (int i=1; (headerName = connection.getHeaderFieldKey(i))!=null; i++) {
if (headerName.equals("Set-Cookie")) {
if (cookie.equals("")) {
cookie = connection.getHeaderField(i);
}
else {
cookie = cookie + "; " + connection.getHeaderField(i);
}
}
}
writeToCookiesFile(cookie);
return cookie;
}
This method was being used in just a plain application though :) Hope it's of some benefit!
The browser will send cookies defined for path /foo only when the path of the url starts with /foo. If a cookie with the same name is set on both / and /foo, there is no way to distinguish them.