AEM code snippets : how to call a servlet using resource type

Here, I am going to explain to you how to a servlet using resource type, this is developed on AEM 6.3, so you need to have below OSGI dependencies in your pom.xml file, if you created your project using AEM project archetype version 12 then you don’t  need to add these, by default it comes

Step 1: Add dependencies

Step 2: Import packages

You need to import below packages in your servlet.class file

Step 3: Create a  servlet

Here, I am not using separate class to load configurations, if you have so many properties to configure then I would recommend you to create a separate class and load it using @Designate annotation, want to know how to use @Designate annotation then refer my other article OSGI annotations – service

here, I am reading  resourceType of a page, check below images, you can get resource type by referring jcr:content node

osgi-annotations-servlet-rstype

In the servlet definition’s you need to add resourceType and selector properties, the selector which I am using here is “demo” and the resourceType is page resourceType

Once the deployment is done, then you can hit the servlet by using below Url

The output should be

osgi-annotations-servlet-rstype-res