Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

Added cookie is not showing properly in the browser.

Avatar

Level 1

Hi,

Am using AEM 6.2 , I have created a servlet which gets invoked on a ajax call,

Below is the way which i have used to add the cookie.

String generatedSession = RandomStringUtils.randomAlphabetic(64);

LOG.info("Generated a_V : "+generatedSession);

Cookie responseCookie = new Cookie("a_v",generatedSession);

responseCookie.setMaxAge(1000);

   responseCookie.setPath("/");

   response.addCookie(responseCookie);

Th above method to add the cookie is not working i.e., i cannot able to see the cookie in the response, nor am not getting any error. I have removed all the blocks to the cookie in my browser,

4 Replies

Avatar

Community Advisor

Hi,

Same code is working for me, can you check try to increase max age just for testing and try to reload page.

String cookieVal = axesCookieVal;

  Cookie axesCookie = new Cookie("myDemoCookie", cookieVal);

  axesCookie.setMaxAge(3600 * 24 * 15 * 1000); // 15 days (for example)

  axesCookie.setPath("/");

  slingResponse.addCookie(axesCookie);



Arun Patidar

Avatar

Level 1

I have used the same but still in the browser cookie i cannot able to find it.

import javax.servlet.http.Cookie;

String generatedSession = RandomStringUtils.randomAlphabetic(64);

LOG.info("Generated a_V : "+generatedSession);

Cookie responseCookie = new Cookie("a_v",generatedSession);

responseCookie.setPath("/");

responseCookie.setMaxAge(3600 * 24 * 15 * 1000); //for 15 days

response.addCookie(responseCookie);

Avatar

Community Advisor

Hi,

I use you code and created Servlet in 6.3 and it is working fine for me

package com.aem.community.core.servlets;

import java.io.IOException;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import javax.servlet.http.Cookie;

import org.apache.commons.lang3.RandomStringUtils;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.osgi.framework.Constants;

import org.osgi.service.component.annotations.Component;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

@Component(service = Servlet.class, immediate = true, property = {

  Constants.SERVICE_DESCRIPTION + "=Simple Set Cookie Servlet", "sling.servlet.paths=/bin/set/cookie", })

public class SetCookieServlet extends SlingSafeMethodsServlet {

  private static final long serialVersionUID = 1L;

  Logger logger = LoggerFactory.getLogger(this.getClass());

  @Override

  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

  throws ServletException, IOException {

  try {

  response.setHeader("Content-Type", "text/html");

  String generatedSession = RandomStringUtils.randomAlphabetic(64);

  logger.info("Generated a_V : "+generatedSession);

  Cookie responseCookie = new Cookie("a_v",generatedSession);

  responseCookie.setMaxAge(3600 * 24 * 15 * 1000); // 15 days (for example)

    responseCookie.setPath("/");

    response.addCookie(responseCookie);

  response.getWriter().close();

  } catch (Exception e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  logger.info(e.getMessage());

  }

  }

}

Screenshot 2018-12-27 at 6.58.28 PM.png



Arun Patidar

Avatar

Level 10

NIce response Arun - as always!!!