Registering Sling Servlets in Adobe Experience Manager
In Adobe Experience Manager (AEM), a Sling servlet can be utilized to handle certain RESTful request-response AJAX calls. Written in the Java programming language, these servlets can be registered as OSGi (Open Services Gateway Initiative) services. There are two methods to register a servlet in AEM: 1) By Path, and 2) By resourceType. Details for both are explained below:
1. Register by Path
For instance, if you want to execute a form POST request to the path /bin/payment
from the client-side to the Sling servlet class, you can use the annotation below:
@SlingServlet(
metatype = true,
methods = { "POST" },
paths = "/bin/payment"
)
public class YourServlet extends SlingSafeMethodsServlet {
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException {
// Perform your tasks here
}
}
When there’s a POST request to http://localhost:4502/bin/payment
, the servlet will be triggered, and the doPost
method will be invoked.
Prerequisites include having a local AEM instance up and running on port 4502 and installing the bundle module via the Maven bundle plugin. You can verify the installation of the bundle by navigating to http://localhost:4502/system/console/bundles
. If it’s not installed, you can manually upload the JAR file.
If you encounter a “forbidden” error and cannot serve the request to /bin/payment
, follow these steps:
- Go to
http://localhost:4502/system/console/configMgr
. - Search for ‘Apache Sling Referrer Filter’.
- Remove the POST method from the filter. This will allow you to trigger the POST method from any source.
- Locate Adobe Granite CSRF Filter.
- Remove POST from the filter methods.
- Save the changes and test the servlet again.
The servlet should now trigger as expected.
2. Register by resourceType
To avoid the issues mentioned above, a better approach is to register the servlet by resourceType. Refactor the servlet as follows:
@SlingServlet(
metatype = true,
methods = { "POST" },
resourceTypes = "services/payment"
)
public class YourServlet extends SlingSafeMethodsServlet {
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException {
// Perform your tasks here
}
}
Next, you’ll need to create a page to trigger this resource:
- Go to CRXDE Lite at
http://localhost:4502/crx/de/index.jsp
. - Inside the
/content
folder, create a page (e.g.,http://localhost:4502/content/submitPage.html
). - In the resourceType properties, enter
services/payment
or whatever matches your servlet above. - Save your changes and test the POST request to
http://localhost:4502/content/submitPage.html
. It should work as expected.
Extra Tips: You can also use the Apache Sling Resource Resolver to verify if the servlet has been registered successfully at http://localhost:4502/system/console/jcrresolver
.
Feel free to leave any questions in the comments below.