May 10, 2020
Estimated Post Reading Time ~

Sling Servlets

Sling Servlets are defined as OSGi services of type javax.servlet.Servlet.
In order to create a Sling Servlet, one of the following classes should be extended:
Class
Usage
SlingAllMethodsServlet

Servlet implementation that responds to all HTTP methods. These are primarily used to surface endpoints that respond to POST (and GET) Requests.
SlingSafeMethodsServlet
This class is intended for applications where data is only read. It supports only GET requests.
AbstractPredicateServlet
Retrieves the predicates from the request.
Servlets can be registered in two ways:
1.   Using path
2.   Using resource type
Java annotation to register a servlet:

1) @SlingServlet(
< Property1>={“values”},
<Property2>={“values”},
…………
)

2)
@Component(metatype = true)
@Service(Servlet.class)
@Properties({
    @Property(name = "sling.servlet.resourceTypes", value = "sling/servlet/default"),
    @Property(name = "sling.servlet.selectors", value = "hello"),
    @Property(name = "sling.servlet.extensions", value = "html"),
    @Property(name = "sling.servlet.methods", value = "GET")
})

Servlets registered by Path:
Service Reference Properties
Description
Way to define
Note
sling.servlet.path
registering the servlet by path
paths={“/a/b”}


For a Servlet registered as an OSGi service to be used by the Sling Servlet Resolver, either or both of the sling.servlet.paths or the sling.servlet.resourceTypes service reference properties must be set. If neither is set, the Servlet service is ignored.

Servlets registered by resource type:
Service Reference Properties
Description
Way to define
Note
sling.servlet.resourceTypes
registering the servlet by resource type
resourceTypes = {“cq:Page”},



If a value for sling.servlet.paths is specified, this is ignored.

sling.servlet.selectors
Filter servlet by selector,
selectors = {“test”}

This property is ignored if the sling.servlet.paths property is set.
sling.servlet.extensions
Filter servlet by extension
extensions = {“xml”}

This property is ignored if the sling.servlet.paths property is set.
sling.servlet.methods

methods = {“GET”, “POST”}
This property is ignored if thesling.servlet.paths property is set. If this property is missing, the value defaults to GET, regardless of which methods are actually implemented/handled by the servlet.
sling.servlet.prefix





By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.