Edit an existing cookie in java - java

I'm tryng to edit the value of an existing cookie, but in vain. I even tried deleting the cold cookie and creating a new one with new value but even that didnt work. Code snippet for editing a cookie:
Cookie modifyCookie= new Cookie(cookie.getName(), newValue)
modifyCookie.setMaxAge(30000)
modifyCookie.setPath("/")
response.addCookie(modifyCookie)
Deleting and creating a new one :
//Deleting the old one
Cookie oldCookie = new Cookie(cookie.getName(), null)
oldCookie.setMaxAge(0);
oldCookie.setPath("/")
response.addCookie(oldCookie);
//Creating a new one
Cookie newCookie = new Cookie(newCookieName, newValue
newCookie.setMaxAge(30000)
newCookie.setPath("/")
response.addCookie(newCookie)
Neither the existing cookie is getting changed nor the old one is getting deleted and new one is getting created
Can you please tell me what is missing?

#rukavitsya
Thanks for your inputs.Issue resolved.
The problem was I was adding cookie to response after the response was sent. Hence cookie was not getting reflected in subsequent requests.
I edited the existing cookie in foll way :
Cookie oldCookie = new Cookie(cookie.getName(), newValue)
oldCookie.setMaxAge(30000);
oldCookie.setPath("/")
oldCookie.setDomain("domain.com")
response.addCookie(oldCookie);
When i edited the cookie before sending the response, things worked as expected.

Related

Cookie cleanup not working with java code, I am trying to remove a cookie from parent domain

I am stuck in cookie clean up issue.
We have created cookie value with domain : .www.parent.com
And later we changed code base to create cookie values in domain : .parent.com
This is giving us cookie values from both the domains and messing up with our code. Is there a way to delete cookies from .www.parent.com via java code ?
I have already tries doing like this :
Cookie cookie = new Cookie("oldCookie" , null);
cookie.setMaxAge(0); or cookie.setMaxAge(-1);
cookie.setPath("/");
response.addCookie(cookie);
You might want to get all the cookies the client has stored and check them, using request.getCookies(), which returns a Cookie array.
In that way, you'll need to check anyone for the wanted domain and set its TTL with something like this:
Cookie[] c=request.getCookies();
for(int i=0;i<c.length;i++){
if(c[i].getDomain().equals(".www.parent.com")){
c[i].setMaxAge(0);
response.addCookie(c[i]);
}
}
This way, the cookie you're passing to the response should have exactly the same name, path and any other attribute except for maxAge value being 0.

Deleting a Cookie from Servlet

I have an application which have a login filter, in which I am creating the cookie like below.
Cookie ck = new Cookie("testCookie","Value");
ck.setPath("/");
response.addCookie(ck);
And in the logout button I am removing the cookie like below.
Cookie ck = new Cookie("testCookie",null);
ck.setPath("/");
ck.setMaxAge(0);
response.addCookie(ck);
But when I try to login again the cookie is existing. I have followed the link
Please let me know how can i remove the cookie completely when i logout from the application.
http://tutorials.jenkov.com/java-servlets/cookies.html
hope it helps. try
Cookie ck = new Cookie("testCookie","");
instead of using null as value
The cookie was not getting deleted because I was using a response.sendRedirect before the response.addCookies.

Cookies not removing by Cookies.removeCookie("cookieName") in GWT

Scenario:
I am setting Cookies in page https://www.example.com/#step1 by using method:
Cookies.setCookies("firstFileName","NewDocument");
Cookies.setCookies("firstFileExt","doc");
Users are supposed to click next and redirect themselves to https://www.example.com/#step2 . But in Case, user click to some other menu (like Home, About us, Contact us), I am deleting these Cookies by using the following method:
Cookies.removeCookie("firstFileName");
Cookies.removeCookie("firstFileExt");
But on removing I found that these two Cookies still holding the values in browser, When I do the following in https://www.example.com/#step1 before setting these two Cookies:
if(!Cookies.getCookie("firstFileName").toString().equals("undefined")){
Window.alert("firstFileName "+firstFileName);
}
I get an alert box that firstFileName NewDocument
Please let me know how can I set and remove the Cookies in my Case.
If you set a cookie without providing a path, as you do using Cookies.setCookies("firstFileName","NewDocument");, the cookie can only be removed on the same page.
A solution would be to always set the cookie for a specific path like that:
// TODO: set expiration time to something more useful
Date expires = new Date();
Cookies.setCookie("firstFileName","NewDocument", expires, null, "/", false);
Then you can remove it by calling that later on:
Cookies.removeCookie("firstFileName", "/");
As your application is based on the relative path "/", every page can set or remove the cookie and all pages access the same cookie value.
If you just set the cookie without providing a path, two pages of your application could even set different cookies.

how to delete the cookies in jsp/java

This my code for setting new cookie
Cookie citizen = new Cookie("citizen",email);
citizen.setMaxAge(3600);
response.addCookie(citizen);
now i'm using this code for destroying the cookie
Cookie[] cookies = request.getCookies();
for(int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals("citizen")) {
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);
}
}
But, i'm still getting cookie value. Help will be appreciated !!
below link might help you..
How can delete information from cookies?
Good Luck!!!
Let me know incase of any further queries...
I was having a problem similar to this, where the cookie retained the value even after setting max age to 0 and the value to "".
I used firefox to look at the cookie attributes to help debug. When logging in, the servlet called my cookie class to set the cookie, and the cookie path was "/javawork/". When logging out, the JSP page called the same cookie class to "delete" the cookie by setting the max age to 0. But the JSP page was in a sub folder in the web app, so the when I created a cookie of the same name with max age of 0, it created a new cookie with the path "/javawork/test_login/".
So that "new" cookie immediately expired, but the original one still existed. In my delete cookie function I needed to set the path of the "new" cookie to be "/javawork/", and when I set the max age to 0 and added it, it updated the original cookie and let me properly log out.
I hope that helps.
The option correct will be
Cookie cookie = new Cookie("citizen", "citizen");
cookie.setMaxAge(0);
cookie.setValue("");
response.addCookie(cookie);
if you try to get the cookie from request for next add it to response with setMaxAge(0), you could see that the cookie doesn't been removed.
Try to add this line:
cookies[i].setMaxAge(0);
//add this line
cookies[i].setPath("/");
response.addCookie(cookies[i]);
This works for me -
Cookie UIDCookie = new Cookie(COOKIE_KEY, "");
UIDCookie.setMaxAge(0);
UIDCookie.setPath("/");
response.addCookie(qptUIDCookie);
We can delete a cookie by setting max age as zero.
For example:
Cookie[] cookies = request.getCookies();
cookies[0].setMaxAge(0);
response.addCookie(cookies[0]);
Here we delete only the first cookie

Cookie handling with Servlet

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.

Categories

Resources