May 13, 2020
Estimated Post Reading Time ~

Sling Servlets

Servlets in AEM is very similar to standard java servlets, however, they can be configured to be used either by everyone on AEM or for a specific component
The default implementation of a servlet is
@SlingServlet(paths = "/bin/sample/getdata", methods = { "GET"})
public class GetDataServlet extendsSlingAllMethodsServlet {
as you can see in the above example the basic implementation will generate a servlet available to be called on /bin/sample/getdata and provides a GET option only. From there the servlet is just like any other piece of code.
You can see that this servlet has extended the SlingAllMethodsServlet, while this is not mandatory and you could just extend Servlet if you wanted, the SlingAllMethods servlet has a lot of helper methods that make it much easier to use.
The SlingServlet annotation allows for multiple ways to be configured
  • generateComponent
    • Whether to generate a default SCR component tag
    • Defaults to true
  • generateService
    • Whether to generate a default SCR service tag
    • Defaults to true
  • paths
    • A list of paths this servlet is accessible on
  • resourceTypes
    • A list of resource types the servlet is available for
  • selectors
    • A list of selectors the servlet is available for
  • extensions
    • A list of extensions the servlet is available for
  • methods
    • A list of the http methods the servlet is available for
  • name
    • The name of the servlet
    • Used for component/service generation
  • metatype
    • If the metatype service data is generated
    • Defaults to false
  • label
    • The label for the servlet
    • Used for component/service generation
  • description
    • The description of the servlet
As you can see there are a few different ways of defining how access to the servlet is done, and you can combine 1 or more of them

path

The simplest definition is the path, while it needs to be a path that is available to the users, or if the servlet is used internally within the AEM application then it can be any url that is not already used by another service within AEM

resourceTypes

Usually, this is the resource type of a specific component, but it doesn't need to be, for internal references for data it can be a predefined label that another component can access. If it is being used to access data for a specific component then the path will be based on the path of the component on the page.
i.e. If you have the page /content/site/lang/home/page1 and on that page, you have a component in a parsys called comp1 then the path to the servlet would be /content/site/lang/home/page1/_jcr_content/par/comp1 there are helpers in sightly to get the current components path

selectors

Selectors are anything between the first and last . in the name of the page you are calling.
i.e. for the above page, if I call /content/site/lang/home/page1.sel1.sel2.sel3.html you are calling the page1 with extension html and with 3 selectors sel1, sel2, sel3

extensions

As in the above selector's example the extension is the piece of code after the last ., but before any other protected characters (/, ?, # e.t.c)
While it is possible to combine both path and resourceTypes these 2 parameters are generally mutually exclusive as they both define the path of the servlet
as an example, you could define a path, selector and extensions
@SlingServlet(paths = "/bin/sample/getdata", selectors="sel1", extensions = "json", methods = { "GET"})
Which would mean to get data you would have to call /bin/sample/getdata.sel1.json while this could all be in the path, it allows for you to have more than one selector or extension
@SlingServlet(paths = "/bin/sample/getdata", selectors={"sel1","sel2"}, extensions = {"json","xml"}, methods = { "GET"})
The slingServlet annotation writes the mapping into the build and at deployment, AEM will read the deployment and register all of the paths that it needs to listen on for the servlet.

SlingAllMethodsServlet

While as I mentioned you don't have to write a servlet as a SlingAllMethodsServlet, it is the easiest and gives you an easier way of implementing your servlet
the SlingAllMethodsServlet provides a nicer way of determining what type of call is being done.
By extending this class all you have to do is override one of the type methods
doGet, doPost, doPut, doDelete, doHead, doOptions, doTrace
As the default servlet is a component, you have access to other services and components via the reference annotation, and it is recommended to have the servlet itself very clean while using a service for processing the of the request
Like all servlets, it is your responsibility to handle getting parameters from the request, and writing the to the response.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.