What is a Servlet?
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.
Servlet declaration and registration
A servlet in AEM can be registered as OSGi service: you can extend SlingSafeMethodsServlet for read-only implementation or SlingAllMethodsServlet in order to implement all RESTFul operations.
You can declare servlet using static paths or resourceTypes (eventually combined with selectors and extensions): you can use both SlingServlet annotations or Component/Service/Properties.
Here two example combining the above options:
@SlingServlet( resourceTypes = "/apps/myproject/components/mycustomresource", selectors = "mycustomselector", extensions = "xml", methods = { "GET" , "POST" }) public class ReadWriteServlet extends SlingAllMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { // GET Implementation } @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { // POST Implementation } }
@Component(metatype = true) @Service(Servlet.class) @Properties({ @Property(name = "sling.servlet.paths", value = "/libs/myservlet"), }) public class ReadOnlyServlet extends SlingSafeMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { // GET Implementation } }
- The
sling.servlet
available configurations are:paths,resourceTypes,selectors,extensions
and
methods.
- Either
sling.servlet.paths
or thesling.servlet.resourceTypes
property must be set, or the servlet is ignored. sling.servlet.paths
must be included in the Execution Paths: their setup is located in “Apache Sling Servlet/Script Resolver and Error Handler” entry of the Web Console Configuration (/system/console/configMgr).sling.servlet.selectors
,sling.servlet.extensions
andsling.servlet.methods
are only considered for the registration withsling.servlet.resourceTypes
.- All configuration values could be a string or an array of strings (see above example for methods).
- The default for
sling.servlet.methods
are GET and HEAD.
First example will respond to: http://localhost:4502/libs/myservlet
Second example will respond to all pages: http://localhost:4502/content/mypage.mycustomselector.xml
having /apps/myproject/components/mycustomresource as its template component page.
CONCLUSIONS
Registering a Servlet by resource type is the preferred approach in Sling, because request handling is independent of where the resources are located. Therefore, there is no need for a predefined static path.
Only with resource type registration you fully utilize the dynamic, resource-oriented approach of the Sling framework: moreover Sling Engine will take care of permissions for you. Users who cannot access a particular resource will not be able to invoke the servlet.
Last but not least, as for all static path, change it (once you already have specified it to your consumers) will reflect in some other changes or broken behaviors.
AUTHORS
Alessandro Paolinelli, Javier Reyes
BIBLIOGRAPHY
Apache Sling – Servlets and Scripts