Skip to main content

Day 06: Playing with Sling Post Servlet

AEM Developer Series

In our previous post, we discussed the Sling Servlets and in the latter part of the post, we created a custom servlet which uses the HTTP GET method.
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.


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. 

Feel free to befriend me on Facebook, Twitter or Linked In or say Hi by email.

Happy Coding 😊


  1. 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?
    Thank you very much for the tutorials!

    1. 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.
      However, 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?

    2. Even after adding cq:dialog node I am still not able to find component on the page.
      Do we need to modify cq.dialog .content.xml ?

    3. Hi Can you please see this post - for the steps to create the cq:Component?

  2. Hi,
    I 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

    1. Hi Atiq, you need to check few things
      1. 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?


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 05: Working with Sling Servlets in AEM

Day 03: Setting up AEM Development Environment