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

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 03: Setting up AEM Development Environment

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 Welcome to the third day of learning AEM development. In previous posts, we discussed the AEM architecture and in this post, we are going to see how to set up an AEM development e