Skip to main content

Day 05: Working with Sling Servlets in AEM


AEM Developer Series

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 technology defines HTTP-specific servlet classes.
All servlets must implement the Servlet interface, which defines life-cycle methods. When implementing a generic service, we can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services.

Sling Servlets

In Sling, servlets can be registered as services. Whenever you create an AEM Multimodule Project (How? see here) using maven archetype 14, the default template for a Sling Servlet is generated as follows - 
Let us discuss its parts one by one

  • @Component annotation - This OSGi annotation signifies that the class is a Service component and is not processed at runtime by a Service Component Runtime (SCR) implementation. It must be processed by tools and used to add a Component Description to the bundle.
  • service property - This signifies the types under which to register this Component as a service. In our case, the component is registered under the Servlet type. 
  • Constants.SERVICE_DESCRIPTION - Defines standard names for the OSGi environment system properties, service properties, and Manifest header attribute keys. In our case, we are defining the description of our servlet.
  • sling.servlet.methods - Defines which methods this servlet will use. In our case, we are using the HTTP GET method.
  • sling.servlet.resourceTypes - This property defines how are we accessing the servlet. There are two ways - via resource types and via paths. We will discuss this in the next section.
  • sling.servlet.extensions - The request URL extensions supported by the servlet for requests. The property value must either be a single String, an array of Strings or a Vector of Strings. This property is only considered for the registration with sling.servlet.resourceTypes.

Servlet Registration

A SlingServletResolver listens for Servlet services and - given the correct service registration properties - provides the servlets as resources in the (virtual) resource tree. Such servlets are provided as ServletResource instances which adapt to the javax.servlet.Servlet class.

For a Servlet registered as an OSGi service to be used by the Sling Servlet Resolver, either one or both of the sling.servlet.paths or the sling.servlet.resourceTypes service reference properties must be set. If neither is set, the Servlet service is ignored. 

A Sling servlet can be registered in two ways - 

  1. Using Resource Types - Using this way, we use the sling:resourceType property of the node. For this, we need to hit the path in the browser for which the sling:resourceType is the given one.
  2. Using Paths - Using this way, we can directly use the path specified in the request and our servlet will be executed.
Registering a servlet via a path is relatively easier than registering it via a resource type but it is still recommended to use resource types instead of paths. Below are the reasons why - 
  • If we use paths, then we need to be extra careful as we do not want to give any path randomly. The servlets cannot be access-controlled using the default JCR ACLs.
  • No suffix handling in the path bound servlets.
  • We also need to specify the paths to the servlet consumers and change in the path can have a serious impact.

Types of Servlets

There are two types of servlets in Sling which are nothing but the classes we need to extend while creating our servlet.
  • SlingSafeMethodsServlet - If we want to use only the read-only methods then we use this. This base class is actually just a better implementation of the Servlet API HttpServlet class which accounts for extensibility. So extensions of this class have great control over what methods to overwrite. It supports GET, HEAD, OPTIONS etc methods.
  • SlingAllMethodsServlet - If we want to use methods that write as well, then we use this. This class extends the SlingSafeMethodsServlet by support for the POST, PUT and DELETE methods.

Sling Servlet Example

In this section, we will be creating our custom Sling Servlet to fetch the JSON data from a RESTful webservice.
As you can see that we are using Network.java and AppConstants.java in the code. This you can find in my previous post.

After deploying the code on your AEM server (how? see here), hit the URL http://<host>:<port>/bin/readjson. You will see JSON string returned on your screen.

Conclusion

Congratulations!! 🙋we have created our first servlet. You can find the complete code at my GitHub
I would love to hear your thoughts on this post and would like to have suggestions from you to make this post better. 
Feel free to befriend me on Facebook, Twitter or Linked In or say Hi by email.

Happy Coding 😊

Comments

  1. Newbie question, we are doing the same thing with OSGI and sling servlet then what exactly is the difference between both of them
    Thanks

    ReplyDelete
    Replies
    1. Hi Shiva,

      As you can see that JSONServlet class is annotated with @Component. A component is the fundamental unit of code in OSGi. Thus, a Sling Servlet is also an OSGi component (in fact, every class in OSGi is a component, or used by a component).

      The concept of Servlets is not specific to OSGi. In Java, Let's say if you want to implement sever-side functionality that deals with HTTP, then we extend our class from HttpServlet In OSGi, we achieve this using Sling Servlets (which extends HttpServlet).

      In a nutshell, a Sling Servlet is also an OSGi component of service type Servlet (see the parameter in the @Component annotation - "service=Servlet.class").

      I hope this is helpful.

      Delete
    2. Thank you for taking your time to explain, helped a lot

      Delete

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