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.
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. 

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

Happy Coding 😊

Comments

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 01: Introduction to AEM

Day 04: Developing first OSGi bundle