AEM sling 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.
Below architecture is all about send data from web browser to servlet program and back from servlet program to web browser.
There are two ways to register servlet:
As part of below servlet implementation, we are going to consume the same project we created as part of this link.
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
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:
Below is complete flow to execute servlet by resourceType.
Step2: 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:
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.
Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.
📝 Blogs
javadoubts.com © All rights reserved