What is Servlet?
- A servlet is a Java class that runs on the server side where the application resides. It acts as a middle layer between the web browser, from which HTTP requests originate, and the application web server.
- Servlets help us process data submitted by end users using POST requests and return data to the web browser using GET requests from the application web server.
- Adobe Experience Manager (AEM) facilitates the creation and deployment of servlet classes on the web server, typically packaged in the form of an OSGi bundle rather than a WAR file, which is more common in traditional Java EE environments.
Over all flow / Architecture
- 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.
How are different ways to register servlet in AEM?
- You can register a Servlet using the two Standard approaches:
1. Registering the servlet by path
2. Register servlet by ResourceType
Difference between PathTypeServlet and ResourceType Servlet?
How many ways to writing servlet?
- Note: Felix Scr is depricated nowadays we are using only Declarative Service(1.2) and Declarative Service(1.2)
How many ways you can extend Servlet?
- 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.
Note:
- @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/declrativeservicer5” ,
“sling.servlet.methods=GET”,
“sling.servlet.extensions=txt”,
property = {
“sling.servlet.paths=/bin/declrativeservicer5” ,
“sling.servlet.methods=GET”,
“sling.servlet.extensions=txt”,
"sling.servlet.selector =" + "aem"
})
})
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.
sling.servlet.resourceTypes This is specifies that the servlet will respond to resources of the type.
sling.servlet.extensions is require to allow number of extensions as part of URL’s.
Basic pseudo code of Declarative Service(1.2)/ OSGi R5 annotaion
- To define a servlet and bind it with a specific path, you use the
sling.servlet.paths
property. For example, to bind your servlet to/bin/declrativeservicer5
, you can configure it as follows: - Note: You can replace
/bin
with any custom path. For instance, if you want to use/aem
instead of/bin
, you can modify the path accordingly:
@Component(service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "=Example Custom Path Servlet",
"sling.servlet.paths=" + "/aem/declrativeservicer5",
"sling.servlet.methods=GET"
})
- If you want to add a resource type to the servlet, you can use the
sling.servlet.resourceTypes
property. For example, to bind your servlet to the resource type/apps/we-retail/components/listwithcontentchanges
, configure it like this:
@Component(service = Servlet.class,
property = { "sling.servlet.resourceTypes=" + "/apps/we-retail/components/listwithcontentchanges",
"sling.servlet.methods=GET"
})
- To define the extension and selector for your servlet, use the
sling.servlet.extensions
andsling.servlet.selectors
properties. For example, to handle requests with the.html
extension and theaem
selector, configure it like this:
@Component(service = Servlet.class,
property = {
"sling.servlet.resourceTypes=" + "/apps/we-retail/components/listwithcontentchanges",
"sling.servlet.extensions=" + "html",
"sling.servlet.selectors=" + "aem"
"sling.servlet.methods=GET"
})
sling.servlet.paths
to bind your servlet to a specific path.sling.servlet.resourceTypes
to bind your servlet to a specific resource type.sling.servlet.extensions
to specify the extension and sling.servlet.selectors
to specify the selector your servlet should handle.How To call your servlet using the GET method in a browser using the pathType?
Basic pseudo code of Declarative Service(1.4)/ OSGi R7 annotaion