Sling Servlets
Part 04 - Registration via Path or Resource Type
- 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 -
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 -
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 |
Reference: Servlets and Scripts
Thanks for the post, but there appears to be no info or tutorial on the web explaining how to use resourceType servlets. Presumably there is some other configuration needed to map the resource type to a path somewhere, but this info is missing from the tutorials. Also if you have 100 end points, its easy to specify the path in each servlet (e.g. /bin/api/login and /bin/api/register, but presumably to do this via resource types requires creating 100 resource types. Also, we need a way to create these resource type mappings in the git source code, creating them on the local running AEM server means it wont get deployed to any other servers or devleopers.
ReplyDeleteHow do you call the resource type path outside AEM? Isn't/bin by default accessible and is the preferred path?
ReplyDelete@Kevin, you can call the resource type servlet outside AEM by this:
ReplyDelete1 - in the content node ( inside CRX/DE) look for the corresponding jcr:ResourceType for the node.
2 - then make the call for that page with selectiors & extension.
Example:
"sling.servlet.resourceTypes="+ "test-website/components/structure/page"
This is for All the pages so to call this servlet:
/content/test-website/test-landing-template/demo-page.txt.html