Retrieving the Current Page in an AEM Servlet via ResourceType

In the context of “Retrieving the Current Page in an AEM Servlet via ResourceType”, it is great to understand how this is done. This guide provides a simplified walkthrough on how you can adeptly achieve this. In the code example, we will associating the servlet with a resourceType.

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
package com.example.aem.servlets;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.osgi.service.component.annotations.Component;
import org.apache.sling.api.resource.ResourceResolver;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;

import javax.servlet.Servlet;
import java.io.IOException;
import java.util.Optional;

@Component(service = Servlet.class)
@SlingServletResourceTypes(
    resourceTypes="example/components/content/mycomponent",
    methods="GET",
    extensions="html"
)
public class ResourceTypeServlet extends SlingAllMethodsServlet {

    @Self
    private SlingHttpServletRequest request;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
        Page currentPage = Optional.ofNullable(request.getResourceResolver().adaptTo(PageManager.class))
                       .map(pm -> pm.getContainingPage(request.getResource()))
                       .orElse(null);

        if(currentPage != null) {
            response.getWriter().write("Gotcha! Current page is: " + currentPage.getPath());
        } else {
            response.getWriter().write("Oops, couldn't find the current page.");
        }
    }
}

Understanding the AEM Code Snippet

1. request.getResourceResolver().adaptTo(PageManager.class):

request.getResourceResolver(): This fetches a ResourceResolver from the servlet’s request. A ResourceResolver in AEM assists in translating resource paths to actual resources.

.adaptTo(PageManager.class): This method tries to adapt or convert the ResourceResolver into another type, specifically a PageManager. The PageManager in AEM offers utilities for working with pages, such as fetching, creating, or deleting pages.


2. Optional.ofNullable(…)

This wraps the value returned by the preceding code (which might be a PageManager object or null if the adaptation failed) within an Optional object. Optional is a container that may or may not contain a value.


3. .map(pm -> pm.getContainingPage(request.getResource()))

If the Optional contains a non-null PageManager value, the .map method applies the given function to it. Here, it uses the PageManager (denoted as pm in the lambda) to retrieve the containing page of the present resource.

pm.getContainingPage(request.getResource()): This extracts the AEM Page that encompasses the current resource.


4. .orElse(null)

Lastly, if any prior steps don’t succeed (i.e., if the adaptation to PageManager was unsuccessful or if no containing page for the resource exists), the orElse method ensures currentPage is set to 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.

Leave a Reply

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


Back To Top
xx1toto
xx1toto
xx1toto, situs toto slot
bacan4d
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
bacansport
bacansport
xx1toto
bacansport
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
slot gacor bacan4d
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
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
xx1toto
bacansport
xx1toto
bacan4d
xx1toto
xx1toto
bacan4d
linkbacan4d
bacan4d
bacan4d
bacansports
bacan4d
xx1toto
xx1toto
bacan4d
bacan4d
scatter hitam
xx1toto
xx1toto
xx1toto
bacan4d
bacan4d slot gacor
bacan4d
xx1toto scatter hitam
xx1toto
bacan4d
bacansport
bacan4d
ts77casino bet 200
bacansport
aplikasi slot
xx1toto scatter hitam
bacan4d
bacan4d login
situs toto
xx1toto
xx1toto
bacansport
bacan4d
xx1toto
slottoto
xx1toto
bacan4d
bacan4d
ts77casino
ts77casino
ts77casino
ts77casino
Scatter Hitam
Situs Toto
toto slot
Scatter Hitam
Situs Toto
toto slot
bacan4d toto terpercaya
scatter hitam
slot mahjong wins 3
mahjong wins 3 black scatter
scatter hitam
ts77casino
ts77casino
tabel shio 2025
bacan4d
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
slot gacor
facebook bacan4d
Demo Slot
Situs Toto
toto togel
Toto slot gacor
slot gacor bett 200
situs toto
situs toto
bacan4d scatter hitam 2025
bacan4d slot bet 300
bacansport
bacan4d situs slot toto gacor
Situs Toto Slot
Bacansports
Bacantoto Situs Toto Slot
xx1toto
xx1toto, situs toto slot
bacan4d
Situs Toto Slot, Situs Togel, toto slot, xx1toto
scatter hitam
Situs Toto Slot, Bandar Togel, Toto macau
Bacan4d Situs Toto Slot Login
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
Situs Toto Slot
Situs Toto Slot
Bacantoto Situs Slot Toto Slot Gacor
xx1toto, situs toto slot
situs toto slot
bacansports
situs toto slot
bacansport