Cookie not retrieved from getCookie method | Community
Skip to main content
September 13, 2023
Solved

Cookie not retrieved from getCookie method

  • September 13, 2023
  • 3 replies
  • 1595 views

We are setting a cookie through a model class:

ecs.setMaxAge(600);
response.addCookie(ecs);

 

I can see the cookie set in the Application cookies in the browser. However, when I try to retrieve this from the Servlet request.getCookie("ecs").getValue() I get a null pointer exception. This cookie is not being retrieved in the servlet. 

 

Kindly help in figuring out what could be the issue.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by sherinregi-1

hi @pixislinger 

can you check the path in the application cookies browser .Since you have not set it explicitely it would take the current path 

You can set the path like below 

cookie.setPath("/");

Then try accessing the servlet it should come up . 

Also use the below snippet in case you want to iterate over the cookies and see if its coming 

 

 // extracted helper method
  private Cookie getCookie(HttpServletRequest request, String cookieName) {
    if (StringUtils.isBlank(cookieName)) {
      return null;
    } else {
      Cookie[] cookies = request.getCookies();
      if (cookies == null) {
        return null;
      } else {
        if (cookies.length > 0) {
          Cookie[] var3 = cookies;
          int var4 = cookies.length;
          for(int var5 = 0; var5 < var4; ++var5) {
            Cookie cookie = var3[var5];
            if (StringUtils.equals(cookieName, cookie.getName())) {
              return cookie;
            }
          }
        }
        return null;
      }
    }
  }

 

https://sourcedcode.com/blog/aem/get-cookie-example-of-aem-servlet-and-sling-model

 

 

Hope it helps

3 replies

sherinregi-1
Community Advisor
sherinregi-1Community AdvisorAccepted solution
Community Advisor
September 13, 2023

hi @pixislinger 

can you check the path in the application cookies browser .Since you have not set it explicitely it would take the current path 

You can set the path like below 

cookie.setPath("/");

Then try accessing the servlet it should come up . 

Also use the below snippet in case you want to iterate over the cookies and see if its coming 

 

 // extracted helper method
  private Cookie getCookie(HttpServletRequest request, String cookieName) {
    if (StringUtils.isBlank(cookieName)) {
      return null;
    } else {
      Cookie[] cookies = request.getCookies();
      if (cookies == null) {
        return null;
      } else {
        if (cookies.length > 0) {
          Cookie[] var3 = cookies;
          int var4 = cookies.length;
          for(int var5 = 0; var5 < var4; ++var5) {
            Cookie cookie = var3[var5];
            if (StringUtils.equals(cookieName, cookie.getName())) {
              return cookie;
            }
          }
        }
        return null;
      }
    }
  }

 

https://sourcedcode.com/blog/aem/get-cookie-example-of-aem-servlet-and-sling-model

 

 

Hope it helps

AshwiniSathe1
September 13, 2023

@pixislinger You need to set the cookie path as "/" while creating the cookie. Try below code to retrieve the cookie.

Cookie[] cookies = request.getCookies();
if (cookies != null) {
        for (Cookie cookie : cookies) {
               if (cookie.getName().equalsIgnoreCase("ecs")) {
                          cookie.getValue();
               }
        }
}
kautuk_sahni
Community Manager
Community Manager
September 14, 2023

@pixislinger  Do you find the suggestions from users useful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. 

Kautuk Sahni