In AEM, a Sling Servlet can be associated with a specific resource type by using the @SlingServlet
annotation and specifying the resourceTypes
property. This allows the servlet to handle requests for a specific type of resource and generate a response based on that resource type.
Here is an example of how to associate a Sling Servlet with a resource type in AEM:
- Create a Java class that extends either the
SlingSafeMethodsServlet
orSlingAllMethodsServlet
classes and override thedoGet
ordoPost
method to handle the HTTP request and generate the HTTP response. - Add the
@SlingServlet
annotation to the servlet class and specify theresourceTypes
property to define the resource type that the servlet should handle. For example:@SlingServlet( resourceTypes = "myapp/components/myComponent", selectors = "mySelector", extensions = "html", methods = "GET" )
In this example, the servlet is associated with the
myapp/components/myComponent
resource type and is configured to handle HTTP GET requests with the.html
extension and themySelector
selector. - Register the servlet as an OSGi service in the
src/main/java/META-INF/services
directory, as described in the previous answer.
Once you have followed these steps, your servlet will be associated with the specified resource type and will be available to handle requests for that resource type. When a request is made for a resource with the specified type, the servlet’s doGet
or doPost
method will be called to generate the response.