Beginners Tutorial: Servlets in AEM

Опубликовано: 20 Октябрь 2024
на канале: Tech Forum
3,692
54

#aem #adobeexperiencemanager #aemdevelopment

Sample Sling Servlets - https://github.com/techforum-repo/you...

A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.

All servlets must implement the Servlet interface, which defines life-cycle methods. The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services.

One of the major differences between the sling servlet container comparing to the regular servlet container is the regular servlet container calls the init life cycle method exactly once after instantiating the servlet.

the container instantiates and initializes the servlets together. With Sling the tasks of instantiation and initialization are split:

The provider of the Servlet service takes care of creating the servlet instance
The Sling Servlet Resolver picks up the Servlet services and initializes and destroys them as needed

So Sling has no way of making sure a Servlet is only initialized and destroyed once in the lifetime of the Servlet object instance.

Servlets are used to expose a URL to the outside world. In AEM the servlets can be registered through OSGI annotations

SlingSafeMethodsServlet

Helper base class for read-only Servlets used in Sling. This base class is actually just a better implementation of the Servlet API HttpServlet class which accounts for extensibility. So extensions of this class have great control over what methods to overwrite.
If any of the default HTTP methods are to be implemented just overwrite the respective doXXX method
this base class is intended for applications where data is only read – GET, OPTIONS, TRACE, and HEAD

SlingAllMethodsServlet

Helper base class for data modifying Servlets used in Sling.
This class extends the SlingSafeMethodsServlet by support for the POST, PUT and DELETE methods.

OptingServlet

OptingServlet interface further refines the servlet request resolution process. It has only one method accepts() which returns a boolean.
If this method returns true then only the servlet (which is implementing this interface) will be selected for the processing.
While an opting servlet seems to be a nice way of picking the right servlet to process the request, the use of an opting servlet is not recommended: the main reason is that it complicates the request processing, makes it less transparent what is going on during a request and prevents optimizations like caching the script resolution in an optimal manner.

SlingHttpServletRequest

Extends javax.servlet.http.HttpServletRequest
The SlingHttpServletRequest defines the interface to provide client request information to a servlet.
Enables additional methods related sling resources – getResource, getResourceResolver, getRequestProgressTracker etc
Enables various parameter accessor methods e.g getRequestParameter

SlingHttpServletResponse

Extends javax.servlet.http.HttpServletResponse
The SlingHttpServletResponse defines the interface to assist a servlet in creating and sending a response to the client.
This interface is currently empty

Registration Properties
sling.servlet.resourceTypes - The resource type(s) supported by the servlet. The property value must either be a single String, an array of Strings
sling.servlet.selectors - The request URL selectors supported by the servlet.
sling.servlet.extensions - The request URL extensions supported by the servlet for requests.
sling.servlet.methods - The request methods supported by the servlet. If sling.servlet.methods are not specified, the servlet is only registered for handling GET and HEAD requests.
sling.servlet.paths - A list of absolute paths under which the servlet is accessible as a Resource.
sling.servlet.resourceSuperType - The resource supertype, indicating which previously registered servlet could intercept the request if the request matches the resource super type better. The property value must be a single String (the default supertype is sling/servlet/default).

Registration Types
Registering by Path

Servlet is registered via a specific path
"sling.servlet.paths=" + "/bin/sampleservlet" 

Registering by Resource Type

Servlet can be registered via resourceType
"sling.servlet.resourceTypes="+ "weretail/components/structure/page“
Servlet is mapped based on the sling:resourceType property of the node