Registering Sling Servlets in Adobe Experience Manager


Hello, and welcome to “Continuous Improvement,” the podcast that provides tips, insights, and strategies for enhancing your skills in software development. I’m your host, Victor.

In today’s episode, we’ll dive into the world of Adobe Experience Manager and explore how to handle RESTful request-response AJAX calls using Sling servlets. We’ll discuss two methods to register these servlets in AEM - by path and by resourceType. So, let’s get started!

Sling servlets, written in Java, are designed to handle specific AJAX calls within AEM applications. They can be registered as OSGi services and are useful for executing various tasks based on incoming requests.

Let’s begin with the first method - registering a servlet by path. Imagine you want to handle a form POST request at the path /bin/payment. To do this, you’ll need to annotate your servlet class using the following code:

[Code Mention]

This annotation ensures that your servlet is triggered when a POST request is sent to http://localhost:4502/bin/payment. The doPost method within the servlet class will be invoked, allowing you to perform your desired tasks.

It’s important to have a local AEM instance running on port 4502 and install the bundle module using the Maven bundle plugin before registering your servlet. You can check if the bundle is installed by navigating to http://localhost:4502/system/console/bundles. If it’s not installed, you can manually upload the JAR file.

Now, what happens if you encounter a “forbidden” error when trying to serve a request to /bin/payment? Don’t worry; I’ve got you covered!

Here’s what you can do:

  1. Go to http://localhost:4502/system/console/configMgr.
  2. Search for ‘Apache Sling Referrer Filter’.
  3. Remove the POST method from the filter. This step allows triggering the POST method from any source.
  4. Locate Adobe Granite CSRF Filter.
  5. Remove the POST method from the filter methods as well.
  6. Save the changes and give your servlet another try.

By following these steps, you should be able to resolve the “forbidden” error and successfully trigger your servlet.

Now, let’s move on to the second method - registering a servlet by resourceType. This approach is more flexible and avoids the aforementioned issues. Here’s how you can do it:

[Code Mention]

Refactor your servlet by using this annotation and specify the desired resourceType. For example, services/payment or any other resourceType that matches your servlet. This way, your servlet will be triggered by requests to pages with the specified resourceType.

To test your servlet, you’ll need to create a page that triggers its resourceType:

  1. Go to CRXDE Lite at http://localhost:4502/crx/de/index.jsp.
  2. Inside the /content folder, create a page, for example, http://localhost:4502/content/submitPage.html.
  3. In the resourceType properties, enter services/payment or the corresponding resourceType from your servlet.
  4. Save your changes and test the POST request by visiting http://localhost:4502/content/submitPage.html. It should work as expected.

An extra tip for you! You can use the Apache Sling Resource Resolver at http://localhost:4502/system/console/jcrresolver to verify if your servlet has been successfully registered.

And that wraps up today’s episode of “Continuous Improvement.” We explored the world of Sling servlets in Adobe Experience Manager, discussing how to register them by both path and resourceType.

Thank you for joining me, Victor, your host, on this journey of continuous improvement. I hope you found today’s episode valuable in expanding your skills as a software developer.

If you have any questions or comments, feel free to reach out in the comments section of the associated blog post.

Don’t forget to subscribe to “Continuous Improvement” for more insightful episodes and updates. Until next time, keep striving for excellence and embracing the world of continuous improvement.