i want to remove cookie with domain name and context path as "/" which is running in my cloud server.
i have below code for clearing cookie in cloud server
Cookie cookie = new Cookie(cookieName, null);// cookieName = TEST_COOKIE
String cookiePath = request.getContextPath();
cookie.setPath(cookiePath); // path = "/"
cookie.setDomain("mydomain.com");
cookie.setMaxAge(0);
response.addCookie(cookie);
if i notice the cookie in my browser i have below details
cookie name = "TEST_COOKIE" value = "MUZJd3NuNDhy" domain = "mydomain.com" path = "/"
where in my localhost, above code works fine, without setting domain name. even i tried with empty domain name which is not working. dont know how to proceed with this, direction is much appreciated.
EDIT - below code without domain in localhost is working fine with context path as /MyApp.
Cookie cookie = new Cookie(cookieName, null);
String cookiePath = request.getContextPath();
cookie.setPath(cookiePath); // path = "/"
cookie.setMaxAge(0);
response.addCookie(cookie);
when i removed contextPath "/MyApp", it stopped working in localhost too, where in my cloud server my context path is "/" .
After lot of debugging, i found the request.getContextPath was returning empty string instead of "/" in my remote server, and in jave doc
* Returns the portion of the request URI that indicates the context
* of the request. The context path always comes first in a request
* URI. The path starts with a "/" character but does not end with a "/"
* character. For servlets in the default (root) context, this method
* returns "". The container does not decode this string.
since i am having root context , the method return empty string instead of "/", i have fixed it by below code and it is working now.
if (cookiePath.isEmpty()) {
cookie.setPath("/");
} else {
cookie.setPath(cookiePath);
}