Servlets

Servlets is a way of accessing the data through request and expects a response in the form of json , text , xml.

Note: 2 things to remember in servlet , registering and consuming the servlet.

Below annotation(OSGI Annotations) are used for a Servlets

@Component(service={Servlet.class}, property={"sling.servlet.methods=post""sling.servlet.extensions=json", "sling.servlet.selectors=home", "sling.servlet.resourceTypes=apps/components/testcomponent", "sling.servlet.paths=/bin/sampleservletPath"})

In AEM servlets are mostly used to get the json data which is stored.  

Servlet can be registered in 2 ways mentioned above (Pink and blue color)

1) With Path

2) With resourceType.

    When regestering with Path, you need to make sure the path is added in Apache Sling Servlet / Script Resolver and Error Handler OSGi configuration as below i.e., path can be added /bin/sampleservletPath.



There might be multiple ways of calling a Servlet, but mainly it can be accessed in 2 ways.

1) Ajax Call  (Hit the Path directly in ajax call or get the resourceType from sightly with ${resource.path} in hidden or data attribute variable and pass to ajax.)  below is the one of the example for calling Ajax.

var resourcePath = $('.resource-path').val(); // this is Path/resourceType Value
$.post({
url: resourcePath + ".selector.extension",
data: passtheinputstoRequest
complete: function (data) {
       processData(data);
                    })

2) Hitting the registered servlet in URL/Postman(This is used only in few cases while executingmanually)

A Servlet have to be extended either with SlingSafeMethodServlet or with SlingAllMethodServlet.  Internally SlingAllMethodServlet extends SlingSafeMethodServlet. Internally SlingSafeMethodServlet extends GenericServlet  Refer: SlingAllMethodsServlet (Apache Sling Aggregate 5-incubator API)

SlingSafeMethodServlet have only 1 method

  • doGet

SlingAllMethodServlet  have below methods.

  • doDelete
  • doGet
  • do
  • doPut
Will write our business logic inside the above methods.

Note: Refer Create servlet | Adobe Experience Manager


Comments

Popular posts from this blog

AEM Developer Series Syllabus

AEM Developer interview Questions

Creating an AEM Project using Maven