Get 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 accessed for an AEM Servlet, and for example two, Sling Model.


1. AEM Servlet Get 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.sourcedcode.core.servlet;

import com.cat.wcm.core.util.JSONObject;
import org.apache.commons.lang.StringUtils;
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 javax.servlet.http.HttpServletRequest;
import java.io.IOException;

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

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

  @Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    Cookie myCookie = getCookie(request, "myCookie");
    String myCookieValue = "";
    if (myCookie != null && !myCookie.getValue().isBlank()) {
      myCookieValue = myCookie.getValue();
    }

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

    JSONObject jsonResponse = new JSONObject();
    jsonResponse.put("myCookie", myCookieValue);
    response.getWriter().write(jsonResponse.toString());
  }
     
  // 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;
      }
    }
  }
}

2. Sling Model Get 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.sourcedcode.models.pages.page;

import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
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;
import javax.servlet.http.HttpServletRequest;

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

  @SlingObject
  protected SlingHttpServletRequest request;

  @PostConstruct
  private void init() {
    Cookie myCookie = getCookie(request, "myCookie");
    String myCookieValue = "";
    if (myCookie != null && !myCookie.getValue().isBlank()) {
      myCookieValue = myCookie.getValue();
    }
  }

  // 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;
      }
    }
  }
}

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.

4 thoughts on “Get Cookie Example of AEM Servlet and Sling Model

    1. Hi there, sorry, I don’t have time to share you HTL code, but how this will work is that if you have a Sightly FE component, which will be rendered apart of the HTML (cached in the dispatcher), I would create JavaScript which will make an aJax call to the AEM servlet, and then use JS to render HTML elements on the page.

    1. It does not get cached if you call it from a servlet. When calling it from a component, and the page is cached, yes thats true. The rendered HTML will be cached apart of the page.

Leave a Reply

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


Back To Top
xx1toto
xx1totol
toto slot
slot gacor
slot pulsa
Slot Dana
slot pulsa
slot pulsa
Situs Toto
situs toto
toto slot
scatter hitam
slot toto
situs toto slot
xx1toto
slot toto
slot toto
Scatter Hitam
Situs toto
Slot Demo
toto slot
Scatter Hitam
bandar togel, situs toto slot
bacan4d
toto slot
Situs Toto Slot
situs slot gacor
situs toto slot
Slot Mahjong
xx1toto
situs slot gacor
Situs Toto
xx1toto
xx1toto, Situs Toto Slot
xx1toto
bacansport
bacan4d
xx1toto, situs toto slot, bandar casino
xx1toto, situs toto slot, bandar casino
xx1toto, situs toto, toto slot
xx1toto, Situs Toto Slot
Link Situs Toto Slot
scatter hitam
xx1toto, situs toto, tabel shio
Situs Scatter Hitam Mahjong Wins 3
xx1toto
xx1toto, situs toto slot, bandar togel
situs toto slot
scatter hitam, black scatter, mahjong wins 3
mahjong scatter
situs toto
black scatter, mahjong wins 3
xx1toto
xx1toto
bacan4d
xx1toto, situs toto
bacansport
bacansport
xx1toto
bacansport
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
slot gacor bacan4d
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
Situs Toto, Toto Slot
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
bacansport
xx1toto
bacan4d
xx1toto
xx1toto
bacan4d login situs toto
situs toto slot
linkbacan4d
bacan4d
bacan4d
bacansports
bacan4dslot gacor<
xx1toto
xx1toto
bacan4d
bacan4d
scatter hitam
xx1toto
xx1toto
xx1toto
bacan4d
bacan4d situs toto macau
bacan4d slot gacor x1000
xx1toto scatter hitam
xx1toto
xx1toto
xx1toto
bacan4d slot gacor 2024
bacansport
bacan4d game slot gacor
ts77casino bet 200
bacansport
aplikasi slot
xx1toto scatter hitam
bacan4d login
bacan4d login situs slot gacor
situs toto
xx1toto
xx1toto
bacansport
bacan4d
xx1toto
xx1toto
slottoto
xx1toto
bacan4d login slot gacor
bacan4d slot
ts77casino
ts77casino
ts77casino
ts77casino
Scatter Hitam
Situs Toto
toto slot
Scatter Hitam
Situs Toto
toto slot
bacan4d situs slot toto
scatter hitam
slot mahjong wins 3
mahjong wins 3 black scatter
scatter hitam
ts77casino
ts77casino
tabel shio 2025
bacan4d slot sever thailand
akun slot bet 400
bacan4d slot gacor toto
instagram bacansports
situs toto
bacan4d slot toto
scatter hitam
Situs Toto
scatter hitam
slot bet kecil
bacansports taruhan bola
taruhan bola
facebook bacan4d
Demo Slot
Situs Toto
toto togel
Toto slot gacor
slot gacor bett 200
situs toto
situs toto
bacan4d situs toto 176
bacan4d slot toto 2025
bacansport
bacantogel
situs slot toto
Situs Toto Slot
Bacansports
Bacantoto Situs Toto Slot
xx1toto
xx1toto
xx1toto, situs toto slot
bacan4d
Situs Toto Slot, Situs Togel, toto slot, xx1toto
scatter hitam
Situs Toto Slot, Bandar Togel, Toto macau
Slot Toto
xx1toto, Login Situs Toto
xx1toto, Situs Toto Slot
Situs Toto Slot, Agen Toto Slot 4D, Tabel Shio
xx1toto, situs toto, toto slot, bandar togel
xx1toto, situs black scatter hitam
Situs Toto Slot, Bandar Togel
xx1toto, situs toto
Situs Toto Slot
Situs Toto Slot
Slot Toto
situs toto slot
bacan4d situs idn slot
bacansports
situs toto slot
bacansport
situs toto slot
situs slot terbaru
agen toto, situs toto
situs toto slot
situs toto slot
situs toto slot
bacan4d
bacan4d
bacan4d
situs toto slot
situs toto slot
Slot Mahjong, Scatter Hitam
xx1toto, situs toto slot
situs toto
situs toto
bandar togel, situs toto slot
toto slot
Bacan4d Login Situs Slot Toto
Bacan4d Login Toto Slot
Bacan4d Situs Toto Slot
Link Alternatif Bacan4d Situs Toto Slot
Bacan4d Slot Gacor
Bacan4d Slot Bet 400
Bacantoto Situs Slot
BACANTOTO Situs Toto
toto slot
bacansport
Toto Slot
Bacan4d login
Slot Toto
BACANSPORTS | Slot Gacor Games
Slot Toto
Slot Toto
bacansport
Slot toto
bacansport
bacansport
bacansport
bacansport
bacansport
Bacan4d Link Alternatif
Bacan4d Rtp Slot
bacan4d
ts77casino
taruhan bola
ubobet bola
bacansport
situs taruhan bola indonesia
situs taruhan bola
Login Toto
taruhan bola
judi bola
taruhan bola
bacan4d
toto slot
bacan4d promo
bacantoto casino slot
bacansport
Bacansports Piala Dunia 2026
bacan4d daftar
bacan4d
bacansport
Slot Dana
bacansports
bacansports
Bacan4d Toto
bacansports
slot dana
Slot Bola
bacantogel
bacantogel
slot toto