Set Cookie Example of AEM Servlet and Sling Model

In this short article, we will show two code examples; for example, one will be showcasing how cookies values can be set for an AEM Servlet, and for example two, Sling Model.


1. AEM Servlet Set Cookie Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.sourcedcode.core.servlets;

import com.cat.wcm.core.util.JSONObject;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;

import javax.servlet.Servlet;
import javax.servlet.http.Cookie;
import java.io.IOException;

import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_PATHS;

@Component(
  service = { Servlet.class },
  property = {
    SLING_SERVLET_PATHS + "=/bin/setCookieExample"
  }
)
public class SetCookieServletExample extends SlingAllMethodsServlet {

  @Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    Cookie cookie = new Cookie("visitedCookieServletExample", "true");
    cookie.setMaxAge(86400); // in seconds, 86400 = 24 hours.
    response.addCookie(cookie);

    // server response
    response.setStatus(200);
    response.setContentType("application/json");

    JSONObject jsonResponse = new JSONObject();
    jsonResponse.put("success", "true");
    response.getWriter().write(jsonResponse.toString());
  }
}

2. Sling Model Set Cookie Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.sourcedcode.models.pages.page;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.DefaultInjectionStrategy;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.SlingObject;

import javax.annotation.PostConstruct;
import javax.servlet.http.Cookie;

@Model(
  adaptables = {SlingHttpServletRequest.class, Resource.class},
  defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class ExampleModel {

  @SlingObject
  protected SlingHttpServletResponse response;

  @PostConstruct
  private void init() {
    Cookie cookie = new Cookie("visitedExampleModel", "true");
    cookie.setMaxAge(86400); // in seconds, 86400 = 24 hours.
    response.addCookie(cookie);
  }
}

Looking for Help? Or need a mentor?

If you need help to super drive your AEM career road map, you can hire me for a one-hour session on codementor.io; We can work together on a price works just for you. https://www.codementor.io/@briankasingli. I'd be happy to help you along your way to becoming a high-quality AEM full-stack engineer.

Was this post helpful?

Hello, I am an enthusiastic Adobe Community Advisor and a seasoned Lead AEM Developer. I am currently serving as an AEM Technical Lead at MNPDigital.ca, bringing over a decade of extensive web engineering experience and more than eight years of practical AEM experience to the table. My goal is to give back to the AEM Full Stack Development community by sharing my wealth of knowledge with others. You can connect with me on LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top