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