Java Beginner Java Beginner - 3 months ago 9
Java Question

JSP Cookie Variable Detected as Boolen rather Than Cookie

I am Setting a Cookie in the Servlet as Below

Cookie NameCookie = new Cookie("Name", "Mugil Vannan");
NameCookie.setMaxAge(30*60);
response.addCookie(NameCookie);

Cookie LocationCookie = new Cookie("Location", "India");
LocationCookie.setMaxAge(30*60);
response.addCookie(LocationCookie);


After setting the cookie I am Trying to access the Particular Cookie set Under Name

Cookie[] cookies = request.getCookies();


for (int i = 0; i < cookies.length; i++)
{
Cookie cookie = cookies[i].getName().equals("Name");

String cookieName = cookie.getName();
String cookieValue = cookie.getValue();
out.print(cookieValue);
}


Now Its getting displayed as TypeMismatch:Cannot Convert Boolean to Cookie at

cookies[i].getName().equals("Name");

Any Idea Why this Happens

Answer

I think this is your problem Cookie cookie = cookies[i].getName().equals("Name");

Did you mean:

Cookie cookie = cookies[i];
if (cookie.getName().equals("Name")) {
    String cookieName  = cookie.getName();
    String cookieValue = cookie.getValue();
    out.print(cookieValue);
}