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
In this post, we are going to create a Sling Post servlet which is invoked by an HTTP POST call. We will make an AJAX call to achieve this.
Since this a POST request, we will be using SlingAllMethodsServlet as a parent to our custom servlet.
Sling Post Servlet Example
in this example, we will make an HTTP post request via AJAX to add a node at a path in the JCR. After adding the node, we will also modify the node by adding a name property.
- Create an AEM Multimodule project in eclipse (how? see here)
- Create a class SampleSlingPostServlet and paste the following code in it.
- Here we are registering the servlet via path /bin/submitdata. Property sling.servlet.methods define the HTTP method to be used i.e. POST in this case. Since we are writing the data, we have extended the class from SlingAllMethodsServlet.
- The logic in the doPost() creates a node under a given path and added a name property in it.
- Now to call this servlet via AJAX, create a new cq:component (how? see here) and in the HTML file of the component, add below code
- Note that the url property has the same path by which the servlet is registered.
- When we drag and drop the component on an AEM page, the post servlet will invoke and a node with name property will be created. You can check the same in CRXDE.
![]() |
CRXDE |
Conclusion
Congratulations!! 🙋 we have created a sling post servlet and invoked it via an AJAX call. I hope you enjoyed this post.
You can find the complete code of this project on my GitHub. Feel free to fork or open issues, if any.
I would love to hear your thoughts on this and would like to have suggestions from you to make it better.
Happy Coding 😊
It seems that some kind of sling:resourceSuperType is needed for it to become visible on the template. In a previous example we used wcm/foundation/components/parsys. What do we use in this case?
ReplyDeleteThank you very much for the tutorials!
Hi, you don't necessarily have to put sling:resourceSuperType in a component to make it work. To be visible on the page (after drag/drop), a component only needs to have cq:dialog node.
DeleteHowever, if you want the component's dialog to have fields then you can add them accordingly.
Can you please elaborate what issue are you facing? Is the component not visible on the page after adding or is some error being thrown?
Even after adding cq:dialog node I am still not able to find component on the page.
DeleteDo we need to modify cq.dialog .content.xml ?
Hi Can you please see this post - https://aem.redquark.org/2018/10/day-07-creating-your-first-component-in.html for the steps to create the cq:Component?
DeleteHi,
ReplyDeleteI have registered my custom servlet with resource type and method as POST . When I am hitting the page which is linked with that resource type the servlet is not getting invoked.
Whereas it is working with GET method.
So any config is missing here can u please help
Hi Atiq, you need to check few things
Delete1. Is your AJAX call using "POST" as method?
2. Have you set your property "sling.servlets.methods=" + HttpConstants.METHOD_POST explicitly?
3. Is "sling.servlet.resourceTypes" set?
Hi Anirudh,
ReplyDeleteI have one doubt here, we will call GET servlet in URL right if we are using path,
how post servlet will be calling and how do we ensure that servlet has been called if we are using paths or resourceTypes?