Apache Sling

Understand Apache Sling — the web framework that powers AEM's URL resolution, request processing, and resource-centric architecture.

Last reviewed: December 2024

·AEM 6.xAEM as a Cloud Service
✅
Quick Understanding: Apache Sling is the web framework that AEM is built on. Its key idea is "resources, not routes" — instead of mapping URLs to controllers, Sling maps URLs to JCR nodes (resources) and then finds the right script to render them based on the resource type.

🔍 What Is It?

Apache Sling is an open-source web framework built on OSGi and JCR. It provides:

  • →URL Decomposition — Parses URLs into resource path, selectors, extension, and suffix
  • →Resource Resolution — Maps URL paths to JCR nodes
  • →Script Resolution — Finds the right rendering script based on sling:resourceType
  • →Sling Models — The annotation-based model framework
  • →Sling Servlets — URL-mapped request handlers
  • →Sling Filters — Request/response interceptors
  • →Event Handling — OSGi event-based processing

🎯 Why Do We Need It?

Sling's resource-centric approach is what makes AEM's content-driven architecture work:

  • →URLs map directly to content — /content/mysite/en/home.html → the home page node
  • →The same content can be rendered differently based on selectors (/en/home.mobile.html)
  • →No routing configuration needed — the URL IS the content address
  • →Developers focus on content and rendering, not URL routing

🏗️ What Happens in Real Projects?

Sling URL Decomposition in Practice:

/content/mysite/en/home.mobile.html

  • →Resource path: /content/mysite/en/home
  • →Selectors: mobile
  • →Extension: html

AEM finds the home node, reads its sling:resourceType, and looks for a script named home.mobile.html or falls back to home.html.

This is how responsive images work — /content/dam/image.thumb.100.100.jpg uses selectors to generate a 100x100 thumbnail.

⚙️ How It Works

Sling Request Processing:

  1. URL arrives: /content/mysite/en/home.html
  2. Sling decomposes URL → resource path + selectors + extension
  3. Sling resolves resource → JCR node at that path
  4. Sling reads sling:resourceType from the node (e.g., mysite/components/page/home)
  5. Sling searches for a rendering script:

- /apps/mysite/components/page/home/home.html (exact match)

- /apps/mysite/components/page/home.html (fallback)

- Walks up sling:resourceSuperType chain

  1. Script executes and returns HTML

💻 Example

Sling URL Decomposition — How Sling breaks down AEM URLs and finds rendering scripts.

text
URL: /content/mysite/en/home.html
├── Resource: /content/mysite/en/home
├── Selectors: (none)
├── Extension: html
└── Suffix: (none)

URL: /content/mysite/en/home.mobile.html
├── Resource: /content/mysite/en/home
├── Selectors: mobile
├── Extension: html
└── Suffix: (none)

URL: /content/dam/mysite/hero.thumb.800.600.jpg
├── Resource: /content/dam/mysite/hero
├── Selectors: thumb.800.600
├── Extension: jpg
└── Suffix: (none)

URL: /content/mysite/en/search.html/results
├── Resource: /content/mysite/en/search
├── Selectors: (none)
├── Extension: html
└── Suffix: /results

Script Resolution for sling:resourceType "mysite/components/hero":
1. /apps/mysite/components/hero/hero.html        ← Exact match
2. /apps/mysite/components/hero.html             ← Fallback
3. Check sling:resourceSuperType for inheritance

🤔 Common Confusions

❓ What is the difference between Sling and Servlet?

Sling IS built on top of Servlets (OSGi HttpService). But Sling adds the resource-centric model on top. A Sling Servlet is a servlet that's registered to handle specific resource types or paths, rather than URL patterns. You can still write traditional servlets in AEM, but Sling Servlets are the preferred approach.

❓ What is sling:resourceSuperType and how does script inheritance work?

If a component's sling:resourceSuperType points to another component, Sling will look for scripts in the parent component if it can't find them in the child. This is how you extend Core Components — your component inherits all scripts from Core and only overrides the ones you change.

🚨 Common Production Issues

404 errors

Sling can't resolve the resource. Check if the content node exists and if the sling:resourceType is correct.

Wrong script rendering

Multiple scripts match the request. Check script resolution order and selector specificity.

Infinite loops

A component includes itself via data-sly-resource without a stopping condition.

Servlet exposes sensitive data

path-based servlets or broad selectors are allowed through Dispatcher without authentication checks.

ResourceResolver leak in servlet or filter

resolver is opened per request and not closed, causing gradual production instability.

✅ Best Practices

Use sling:resourceSuperType for component inheritance rather than copying scripts.

Use selectors for alternate renderings of the same resource (mobile, print, JSON).

Register Sling Servlets by resource type, not by path

path-based registration is fragile.

Use Sling URL Builder APIs to generate URLs

don't concatenate strings.

Validate selectors, extensions, and suffixes explicitly in servlets so unexpected URL shapes do not bypass business rules.

Use service users for repository access from servlets and filters; do not reuse the request user for privileged background work.

🏛️ Architect's Note

Senior Architect's Perspective

Sling's resource-centric model is elegant but requires a mental shift for developers coming from MVC frameworks. The key insight: the URL is the content address, not a route to a controller.

This model scales beautifully for content-heavy sites — thousands of pages all use the same rendering scripts, but with different content. No routing configuration needed.

💬 Frequently Asked Questions

What is Sling Resource Merger?

Sling Resource Merger allows overlaying content from /libs with content in /apps. When you create a node at /apps/path/that/mirrors/libs, Sling merges the two, with /apps taking precedence. This is how AEM overlays work.

🎯 Key Takeaways

1Sling = resource-centric web framework — URLs map to JCR nodes, not controllers
2URL decomposition: path + selectors + extension + suffix
3sling:resourceType tells Sling which script to use for rendering
4Script resolution walks up sling:resourceSuperType for inheritance
5Selectors enable alternate renderings of the same resource

🎯 Practice this topic in interviews

↗ Where this topic fits

Sources

Sources / References

Use these AEM references to verify the concept in official docs and continue into the most relevant SplashRide pages.

Related SplashRide pages

SplashRide AEM learning path

Browse the full AEM topic collection and learning order on SplashRide.

AEM Interview Prep

Practice AEM interview questions and scenario-based answers.

AEM Developer Career Path

This AEM developer career path explains how to become an AEM developer through JCR, Sling, HTL, client libraries, Dispatcher, Cloud Service delivery, project depth, and interview-ready enterprise implementation skills.

Drupal Developer Career Path

A practical route through Drupal content modeling, site building, theming, modules, configuration management, APIs, performance, deployment discipline, and interview preparation.

JCR (Java Content Repository)

Learn what the JCR is, how AEM uses it to store everything, and why understanding it is fundamental to AEM development.

Sling Models

Understand Sling Models — the backbone of AEM component logic. Learn how they work, how to write them, and the gotchas that trip up every developer.