Skip to content

2020

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.

在Adobe Experience Manager中註冊Sling Servlets

在Adobe Experience Manager (AEM) 中,Sling servlet可以被用來處理某些RESTful的請求-回應的AJAX調用。寫在Java編程語言中的Servlets可以被註冊為OSGi(開放服務網關協議)服務。在AEM中註冊servlet有兩種方法:1) 通過路徑,和 2) 通過資源類型。以下是兩種方法的詳細說明:

1. 通過路徑註冊

例如,如果您希望從客戶端發送一個表單POST請求到路徑/bin/payment 到Sling servlet類,您可以使用以下的註解:

@SlingServlet(
    metatype = true,
    methods = { "POST" },
    paths = "/bin/payment"
)
public class YourServlet extends SlingSafeMethodsServlet {

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException {
        // 在這裡執行你的任務
    }
}

當有一個POST請求到 http://localhost:4502/bin/payment 時,servlet將被觸發,並調用 doPost 方法。

前提條件包括在端口4502上運行本地AEM實例,並通過Maven Bundle插件安裝bundle模組。您可以通過導航到 http://localhost:4502/system/console/bundles來驗證bundle的安裝。如果沒有安裝,您可以手動上傳JAR文件。

如果您遭遇“禁止”的錯誤,無法服務 /bin/payment 的請求,請按照以下步驟操作:

  1. 轉到 http://localhost:4502/system/console/configMgr
  2. 搜索 'Apache Sling Referrer Filter'。
  3. 從篩選器中刪除POST方法。這將允許您從任何來源觸發POST方法。
  4. 搜索Adobe Granite CSRF Filter。
  5. 從篩選方法中刪除POST。
  6. 保存更改並再次測試servlet。

Servlet現在應該按預期觸發。

2. 通過資源類型註冊

為了避免上述問題,更好的方法是通過資源類型註冊servlet。重構servlet如下:

@SlingServlet(
    metatype = true,
    methods = { "POST" },
    resourceTypes = "services/payment"
)
public class YourServlet extends SlingSafeMethodsServlet {

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException {
        // 在這裡執行你的任務
    }
}

接下來,您需要創建一個頁面來觸發此資源:

  1. 轉到 http://localhost:4502/crx/de/index.jsp的CRXDE Lite。
  2. /content資料夾內,創建一個頁面(例如,http://localhost:4502/content/submitPage.html)。
  3. 在resourceType屬性中,輸入 services/payment 或其他匹配您上述的servlet。
  4. 保存更改並測試POST請求到 http://localhost:4502/content/submitPage.html。 應該可以按预期工作。

額外提示: 您也可以使用Apache Sling Resource Resolver來驗證servlet是否已成功註冊在http://localhost:4502/system/console/jcrresolver

如果有任何問題,可以在下面的評論中留言。