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,
Views
Replies
Total Likes
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);
Views
Replies
Total Likes
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);
Views
Replies
Total Likes
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());
}
}
}
Views
Replies
Total Likes
NIce response Arun - as always!!!
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies