AEM Sling Servlet
Servlet is a Java class which runs on a server side where application resides. This is a middle layer between web browser from where HTTP request originate and application web server.
Servlet helps us to get data submit by end users using POST request and return data to web browser using GET request from application web server.
Adobe Experience Manager helps us to create servlet classes and deploy on web server in the form of war file.
Below are the two main Java Servlet classes are available to implement servlet classes.
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
SlingSafeMethodServlet provides a capability to create read only servlets. It only supports GET operation.
SlingAllMethodServlet provides a capability to create both read and write servlet. It supports all operations such as GET, POST, PUT, DELETE, etc.
Over all flow / Architecture
Below architecture is all about send data from web browser to servlet program and back from servlet program to web browser.
- The request will get originate from web browser to get and send data from to server.
- Request will use HTTP or HTTPS protocol to get or send data to Dispatcher.
- The request from web browser will first reach to dispatcher. Dispatcher will return cached data if found. Else it will reach out to AEM instance.
- AEM instance will reach out to specific servlet class depending on servlet URL mapping, request type(GET, POST, DELETE etc. ), content type etc.
- Web browser can send or get data from server in predefined formats such as JSON, XML, binary etc.
- Servlet class will collect data from web browser. Servlet class will process the data collected from browser. Servlet class will and save/store same data in database. Servlet class can also collect data from database and send back to browser in same predefined format.
- Servlet class will process data using third party API data and other classes within application server.
There are two ways to register servlet:
- Using path
- Using ResourceType
As part of below servlet implementation, we are going to consume the same project we created as part of this link.
Using Paths
Using path property we can register servlet in AEM. Path Servlet will directly call Servlet class. But, it will call resource first and than servlet in case of resourceType servlet.
@Component annotation can be use to register a servlet.
Below is the syntax to create a servlet by path.
@Component(service = { Servlet.class },
property = {
“sling.servlet.paths=/bin/practice” ,
“sling.servlet.methods=GET”,
“sling.servlet.extensions=txt”
})
public class SlingServletByPath extends SlingSafeMethodsServlet {
sling.servlet.paths helps us to provide path to access servlet.
sling.servlet.methods is responsible to declare servlet method as Get, Post, Delete, etc.
sling.servlet.selectors is a special form of parameter which help us to pass data to backend servlet code. Below is the example where we are passing John to backend servlet code as part of URL.
http://localhost:4502/bin/practice.John.txt
It is recommended to use selectors in place of request parameter.
Not Recommended: http://localhost:4502/bin/practice.txt?name=John
It is most important servlet property as it helps us to
sling.servlet.extensions is require to allow number of extensions as part of URL’s.
.txt, .json, and .html are the examples of extensions define as part of below URL.
http://localhost:4502/bin/practice.txt
http://localhost:4502/bin/practice.json
http://localhost:4502/bin/practice.html
Below is the code snippet to achieve the same:
OUTPUT
User ResourceType
Below is complete flow to execute servlet by resourceType:
Step1: Hit below URL in browser
http://localhost:4502/content/practice/us/en/_jcr_content.txt
Step2: As soon as we hit servlet path in browser, it will look for /content/practice/us/en/_jcr_content content jcr:content page node.
Step3: On jcr:content node we have sling:resourceType property having value as servlet path to call servlet by resourceType.
It is called as resourceType servlet as we are accessing servlet with the help of resourceType or via resourceType.
Below is the code snippet to achieve the same:
OUTPUT
Note: As path of GET method, it is not allowed to send large amount of data as part of URL. There is a limitation to send maximum of 2048 characters as part of URL.
To resolved above issue, we have POST method servlet to send large amount of data as part of request body.
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.