Part 06 - Default and Opting Servlets
A default servlet is selected if no servlet or script for the current resource type can be found. This servlet is registered with a special resource type sling/servlet/default.
The default servlet is resolved exactly the same way as for any resource type. That is, also for the default Servlet selection, the request selectors and extension or method are considered.
Also, the servlet may be a servlet registered as an OSGi service or it may be a script stored in the repository or provided by any bundle.
Finally, if not even a registered default Servlet may be resolved for the request because none has been registered, the servlets/resolver bundle provides a fall back the DefaultServlet with the following functionality:
- If a NonExistingResource was created for the request, the DefaultServlet sends a 404 (Not Found)
- Otherwise, the DefaultServlet sends a 500 (Internal Server Error), because normally at least a NonExistingResource should be created.
This interface further refines the servlet request resolution process. It has only one method accepts() which returns a boolean. If this method returns true then only the servlet (which is implementing this interface) will be selected for the processing.
It seems that an opting servlet is a nice way of picking the right servlet to process the request, even then it is not recommended as it complicates the request processing.
This makes it less transparent and one may end up wondering what is going on during a request. It also prevents optimizations like caching the script resolution in an optimal manner. Hence, use it very carefully and only when it is absolutely needed.
In this post, we learned about two specific Sling Servlets - Default and Opting Servlet.