how to delete the cookies in jsp/java - 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

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.

How to check whether a cookie has been set on previous visit?

In a java web application,I want to check whether a user who signs in is a returning user. How can I check if there is already a cookie that has been set on earlier login.
On HttpServletRequest you have a getCookies() method that will give you an array of the cookies the client is sending with his request.
http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getCookies%28%29
Set cookie when user performs log-in:
Cookie c = new Cookie("visit", "old")
c.setMaxAge(3600*24*365*1000); // 1 year (for example)
response.addCookie(new Cookie("visit", "old"));
Now you can check this cookie when user with new session comes to the system: request.getCookies(), then iterates over returned array and find "your" cookie. If cookie exists this is "old" user otherwise the new one.

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.

Cookie not being read

I implemented "Remember me" functionality in my web app. I did this using a cookie that contains username/password encrypted using RSA.
I add the cookie when I login; if then I logout (without closing browser) the cookie is read ok and in the login page I see username/pass already typed.
But if I close the browser; or close tab and run the application again, when the cookies are read, the only cookie that is read is the JSESSIONID. the cookie with the credentials is not in the array returned by
((HttpServletRequest)facesContext.getExternalContext().getRequest()).getCookies(­);
even though I can see it in the browser. why is that?
This is the code that creates the cookie:
String credentials = username + "?" + password;
Cookie c = CookieHandler.getInstance().createCookie("vtcred", credentials, rememberMe);
FacesContext facesContext = FacesContext.getCurrentInstance();
((HttpServletResponse) facesContext.getExternalContext().getResponse()).addCookie(c);
and method createCookie:
public Cookie createCookie(String name, String value, boolean rememberMe) {
value = encript(value);
Cookie credCookie = new Cookie(name, value);
credCookie.setHttpOnly(true);
if(rememberMe) {
credCookie.setMaxAge(86400);
}
else {
credCookie.setMaxAge(0);
}
return credCookie;
}
Edit: I am setting the cookie's max age to one day; and in the browser I can see that the cookie expires tomorrow, so that's not the problem
Thanks in advance,
Damian
edit2: this is very odd, but it seems to be working now. I'll keep testing it and notify. Thanks.
I found why sometimes a cookie is not read. It has to do with the path attribute.
If anyone is having this issue, set the path of the cookie, like this:
Cookie c = new Cookie("name", "value");
cookie.setMaxAge(86400);
cookie.setPath("/");
Regards
You might want to set the cookie with an expiration date. If you dont , it will only last as long as the browser session.

Categories

Resources