In this article, I am going to explain to you how to call a servlet using resource type, this is developed on AEM 6.3, so you need to have below OSGI dependencies entries in your pom.xml file, if you already created your project using AEM project archetype version 12 then you don’t need to add these and by default it comes.
Step 1: Add dependencies
<plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>3.3.0</version> <inherited>true</inherited> </plugin>
<!-- OSGi Dependencies --> <dependency> <groupId>org.osgi</groupId> <artifactId>osgi.core</artifactId> <version>6.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>osgi.cmpn</artifactId> <version>6.0.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.osgi</groupId> <artifactId>osgi.annotation</artifactId> <version>6.0.1</version> <scope>provided</scope> </dependency>
Step 2: Import packages
You need to import below packages in your servlet.class file
import org.osgi.framework.Constants; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Modified; import org.osgi.service.metatype.annotations.AttributeDefinition; import org.osgi.service.metatype.annotations.Designate; import org.osgi.service.metatype.annotations.ObjectClassDefinition;
Step 3: Create a servlet
Here, I am not using a 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. if you 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
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
package com.aem.toolkit.core.servlets; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletException; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.SlingHttpServletResponse; import org.apache.sling.api.servlets.HttpConstants; import org.apache.sling.api.servlets.SlingSafeMethodsServlet; import org.osgi.framework.Constants; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Modified; import org.osgi.service.metatype.annotations.AttributeDefinition; import org.osgi.service.metatype.annotations.Designate; import org.osgi.service.metatype.annotations.ObjectClassDefinition; @Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Simple HTML Demo Servlet", "sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.selectors=demo", "sling.servlet.resourceTypes=" + "examples/components/structure/page", "sling.servlet.extensions=" + "html" } ) @Designate(ocd = ResourceTypeServlet.Configurations.class) public class ResourceTypeServlet extends SlingSafeMethodsServlet { private static final long serialVersionUID = 1L; @ObjectClassDefinition(name = "Resource Type Demo Servlet") public @interface Configurations { @AttributeDefinition(name = "Enter Path", description = "Page or node path") String getPath(); } @Override protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType("text/html"); out.write("<html><body>"); out.write("<h1>Welcome to keys and strokes technologoy blog</h1>"); out.write("</html></body>"); out.flush(); out.close(); } @Activate @Modified protected void Activate(Configurations config) { } }
Once the deployment is done, then you can hit the servlet by using below Url
http://localhost:4506/content/examples/en/content/jcr:content/.demo.html
The output should be
Hi Raj!
How about post method ?