Skip to main content

Posts

Showing posts from July, 2019

Sling Servlet 06 - Default and Opting Servlets

Sling ServletsPart 01 - What is a Servlet? Part 02 - What is a Sling Servlet Part 03 - Servlet Resource Provider Part 04 - Registration via Path or Resource Type Part 05 - Types of Servlets Part 06 - Default and Opting Servlets
In this post, we are going to discuss two important types of servlets in AEM - Default and Opting servlets, their uses and pros and cons.
Default Servlet A default servlet is selected if no servlet or script for the current resource type can be found. This servlet is registered with a special resource type sling/servlet/default.
The default servlet is resolved exactly the same way as for any resource type. That is, also for the default Servlet selection, the request selectors and extension or method are considered.
Also, the servlet may be a servlet registered as an OSGi service or it may be a script stored in the repository or provided by any bundle.
Finally, if not even a registered default Servlet may be resolved for the request because none has been registered…

Sling Servlets 05: Types of Servlets

Sling ServletsPart 01 - What is a Servlet? Part 02 - What is a Sling Servlet Part 03 - Servlet Resource Provider Part 04 - Registration via Path or Resource Type Part 05 - Types of Servlets Part 06 - Default and Opting Servlets Types of Sling Servlets There are two classes in Sling API which can be inherited to define our custom functionality. These two classes are - SlingSafeMethodsServlet [org.apache.sling.api.servlets.SlingSafeMethodsServlet]SlingAllMethodsServlet [org.apache.sling.api.servlets.SlingAllMethodsServlet] Let us discuss these types one by one SlingSafeMethodsServlet This is a helper base class for read-only Servlets used in Sling. This base class is actually just a better implementation of the Servlet API's 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 override the respective doXXX (doGet(), doHead() etc.) method.
This base …

Sling Servlet 04 - Registration via resourceType or Path

Sling ServletsPart 01 - What is a Servlet? Part 02 - What is a Sling Servlet Part 03 - Servlet Resource Provider Part 04 - Registration via Path or Resource Type Part 05 - Types of Servlets Part 06 - Default and Opting Servlets
From the previous post, we know that there are two ways by which a sling servlet can be registered -
Servlet registration via resourceType.Servlet registration via paths. In this post, we will see which approach is better and why. Registration via resourceType A servlet can be registered via resourceType using the following property - "sling.servlet.resourceTypes="+"redquark/servlets/example" Here, whenever the request executes anything with the specified resourceType, the servlet registered with it will be executed. This property can accept a String, an array of Strings or a Vector of Strings. Registration via path A servlet can be registered via resourceType using the following property - "sling.servlet.paths="+"/bin/registeredbyp…

Sling Servlet 03: Servlet Resource Provider

Sling ServletsPart 01 - What is a Servlet? Part 02 - What is a Sling Servlet Part 03 - Servlet Resource Provider Part 04 - Registration via Path or Resource Type Part 05 - Types of Servlets Part 06 - Default and Opting Servlets Servlet Resource Provider A service listener org.apache.sling.servlets.resolver.internal.SlingServletResolver listens for the OSGi services implementing javax.servlet.Servlet interface and registers its reference.

When a request hits the server, this SlingServletResolver kicks in to execute its sling script resolution procedure to choose the right servlet to respond to the request.

Each individual servlet will have a dedicated service instance of ServletResourceProvider associated with it, which will provide ServletResources in the resource tree, based on the servlet's registration properties.

The actual resource path of such resources differs for servlets registered by type and those registered by path:

Servlet registered by Full Resource Path Path (given path)…

Sling Servlets 02: What is a Sling Servlet?

Sling ServletPart 01 - What is a Servlet? Part 02 - What is a Sling Servlet Part 03 - Servlet Resource Provider Part 04 - Registration via Path or Resource Type Part 05 - Types of Servlets Part 06 - Default and Opting Servlets What is a Sling Servlet? A Servlet is a class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model.
For such applications, Servlet technology defines HTTP-specific servlet classes.
All servlets must implement the Servlet interface, which defines life-cycle methods. When implementing a generic service, we can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services.
Sling servlets are a special type of servlets which are registered as OSGi service of type javax.servlet.Servlet. There are some properties defined for Sling Servlets which are as follows - sling.servlet.paths - T…

Sling Servlets 01 - What is a Servlet?

Sling Servlets Part 01 - What is a Servlet? Part 02 - What is a Sling Servlet Part 03 - Servlet Resource Provider Part 04 - Registration via Path or Resource Type Part 05 - Types of Servlets Part 06 - Default and Opting Servlets What is a servlet? A servlet in its very core is a Java class, which can handle network requests (for e.g. HTTP requests). Servlets are usually used to implement web applications. This java class does not have any main() method, only some callback methods.
Servlets run on the java enabled web-server or the application server. They handle requests from the web server, process it, produce the response, then send the response back to the server. This means servlet lives and dies within a web container. A web container is responsible for invoking methods in a servlet. It knows what callback methods a servlet has.
Servlets run in a servlet container which handles the networking side (e.g. parsing an HTTP request, connection handling etc.). One of the best known open so…