Sling Servlet Registration Methods - Adobe AEM / CQ
There are two ways to register a Sling Servlet according to the documentation
First @SlingServlet
@SlingServlet(
resourceTypes = "sling/servlet/default",
selectors = "hello",
extensions = "html",
methods = "GET")
public class MyServlet extends SlingSafeMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
...
}
}
Second use @Properties
@Component(metatype = true)
@Service(Servlet.class)
@Properties({
@Property(name = "sling.servlet.resourceTypes", value = "sling/servlet/default"),
@Property(name = "sling.servlet.selectors", value = "hello"),
@Property(name = "sling.servlet.extensions", value = "html"),
@Property(name = "sling.servlet.methods", value = "GET")
})
public class MyServlet extends SlingSafeMethodsServlet {
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
...
}
}
The documentation doesn't show any advantage of taking a specific approach to others. One of my team members asked about this. The only thing I could think of is @Properties
giving us custom properties that can be changed from the OSGi console ( http: // localhost: 4502 / system / console / components , assuming the CQ is running locally on port 4502). Is there a difference or advantage to using a specific approach?
source to share
@SlingServlet
reduces the template needed to create a servlet. Some of the properties you listed as methods = "GET"
are already set by default, so you don't need to reconfigure it. All this makes the annotation more concise.
Internally, like all SCR annotations, the annotation is translated into an XML file, so you won't be able to tell how the servlet was implemented simply by looking at the deployed code instance.
There is nothing to limit you to adding additional @Property
definitions if you want to provide a description of a provider or service. Reason, the latter is indeed available in the annotation @SlingServlet
so again - you will immediately benefit from using it!
I also recommend that you read an excellent presentation from my college that outlined some of the best AEM development practices (including your case @SlingServlet
). You can find here
source to share