Skip to main content

Sling Servlet 04 - Registration via resourceType or Path

Sling Servlets

From the previous post, we know that there are two ways by which a sling servlet can be registered -
  • Servlet registration via resourceType.
  • Servlet registration via paths.
In this post, we will see which approach is better and why.

Registration via resourceType

A servlet can be registered via resourceType using the following property -
"sling.servlet.resourceTypes="+ "redquark/servlets/example"
Here, whenever the request executes anything with the specified resourceType, the servlet registered with it will be executed. This property can accept a String, an array of Strings or a Vector of Strings.

Registration via path

A servlet can be registered via path using the following property -
"sling.servlet.paths=" + "/bin/registeredbypathdemo"
Whenever we hit the request containing the specified path - this servlet will be executed. The value can either be a single String, an array of Strings or a Vector of Strings.
Whenever a servlet is registered with both path and resourceType, the preference is given to the path.

Caveats when binding servlets with the path

Binding servlets by paths have several disadvantages when compared to binding by resourceTypes
  • Path bound servlets cannot be access-controlled using the default JCR repository ACLs while the resourceType bound servlets can be controlled.
  • No suffix handling when bound by path
  • If a path bound servlet is not active, e.g. if the bundle is missing or not started, a POST request might result in unexpected results, usually creating a node at /bin/xyz or many such issues.
  • The mapping is not transparent to the developer who is just looking at the repository.
When we register a servlet using path, we must be specific what all paths are allowed as if we define something randomly, our servlet might not work or give unexpected results.

Only a limited set of paths are allowed and rest are blocked. Though we can add more paths in the OSGi configuration Apache Sling Servlet / Script Resolver and Error Handler.

Apache Sling Servlet / Script Resolver and Error Handler


In this short theoretical post, we discussed the details of how we can register a sling servlet.

I hope you enjoyed this post. Your suggestions and feedback are always welcome. Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Happy Learning 😊


Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.
Adobe is way ahead in this war and has gained a lot of market capture. They are leaders in the Digital Marketing platforms since 2014-15. One of the flagship product in Adobe's Digital Marketing suite is Adobe Experience Manager (AEM).
Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.
When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the res…

Day 05: Working with Sling Servlets in AEM

Day 03: Setting up AEM Development Environment