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

Conclusion

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 😊

Comments

  1. 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.

    ReplyDelete
  2. How do you call the resource type path outside AEM? Isn't/bin by default accessible and is the preferred path?

    ReplyDelete
  3. @Kevin, you can call the resource type servlet outside AEM by this:
    1 - 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

    ReplyDelete

Post a Comment

Popular posts from this blog

Day 00: AEM Developer Series

image source:  https://solutionpartners.adobe.com/ 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

Day 05: Working with Sling Servlets in AEM

AEM Developer Series Day 00: AEM Developer Series Day 01: Introduction to AEM Day 02: AEM Architecture Day 03: Setting up AEM Development Environment Day 04: Developing First OSGi Bundle Day 05: Working with Sling Servlets in AEM Day 06: Playing with Sling Post Servlet Day 07: Creating your first component in AEM Day 08: Dueling with JavaScript Use API Day 09: Dueling with Java User API Day 10: Getting to know Sling Models Day 11: Client Libraries in Action Day 12: Creating your custom OSGi Configuration Day 13: Schedulers in AEM Day 14: Eventing in AEM Day 15: Custom Workflows in AEM Day 16: Creating JMX Beans in AEM Day 17: Working with QueryBuilder API Day 18: Working with Granite Datasources in AEM Day 19: Replication API in Action Day 20: Working with Users and Groups in AEM A Servlet is a class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. For such applications, Servlet tec

Day 12: Creating your Custom OSGi configuration

AEM Developer Series Day 00: AEM Developer Series Day 01: Introduction to AEM Day 02: AEM Architecture Day 03: Setting up AEM Development Environment Day 04: Developing First OSGi Bundle Day 05: Working with Sling Servlets in AEM Day 06: Playing with Sling Post Servlet Day 07: Creating your first component in AEM Day 08: Dueling with JavaScript Use API Day 09: Dueling with Java User API Day 10: Getting to know Sling Models Day 11: Client Libraries in Action Day 12: Creating your custom OSGi Configuration Day 13: Schedulers in AEM Day 14: Eventing in AEM Day 15: Custom Workflows in AEM Day 16: Creating JMX Beans in AEM Day 17: Working with QueryBuilder API Day 18: Working with Granite Datasources in AEM Day 19: Replication API in Action Day 20: Working with Users and Groups in AEM We all know that AEM works on Apache Felix  which is an implementation of OSGi . OSGi provides a way to manage bundles and configurations. We saw one example of an OSGi configurati