April 10, 2020
Estimated Post Reading Time ~

Sling Servlet 04 - Registration via resourceType or Path

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


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.