Skip to main content

Sling Servlet 06 - Default and Opting Servlets


Sling Servlets


In this post, we are going to discuss two important types of servlets in AEM - Default and Opting servlets, their uses and pros and cons.

Default Servlet

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.

Opting Servlet

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.

Conclusion

In this post, we learned about two specific Sling Servlets - Default and Opting 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.

Comments

Post a Comment

Popular posts from this blog

Day 00: AEM Developer Series

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 so much going on under the hood. I then spent months to gather all the res…

Day 01: Introduction to AEM

Day 04: Developing first OSGi bundle