Skip to main content

Sling Servlets 05: Types of Servlets


Sling 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 class is intended for applications where data is only read. As such, this servlet by itself does not support the POST, PUT and DELETE methods. Extensions of this class should either override any of the doXXX methods of this class or add support for other read-only methods only.
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Anirudh Sharma
 */
@Component(service=Servlet.class,
property={
  Constants.SERVICE_DESCRIPTION + "=Simple Read Only Demo Servlet",
  "sling.servlet.methods=" + HttpConstants.METHOD_GET,
  "sling.servlet.resourceTypes="+ "redquark/servlets/example",
  "sling.servlet.extensions=" + "html"
 })
public class SlingSafeMethodsServletEsample extends SlingSafeMethodsServlet {

 private static final long serialVersionUID = 8565932343267006374L;
 
 private final Logger log = LoggerFactory.getLogger(this.getClass());
 
 @Override
 protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
  log.info("Inside the doGet method of the SlingSafeMethodsServlet type servlet");
  try {
   PrintWriter out = response.getWriter();
   out.println("Read only servlet got executed!!!");
  } catch (IOException e) {
   log.error(e.getMessage(), e);
  }
 }
}

SlingAllMethodsServlet

This is a helper base class for data modifying Servlets used in Sling. This class extends the SlingSafeMethodsServlet by support for the POST, PUT and DELETE methods. Thus, if we want to modify some data then we should use this base class.
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Anirudh Sharma
 */
@Component(service=Servlet.class,
property={
  Constants.SERVICE_DESCRIPTION + "=Simple Read + Write Demo Servlet",
  "sling.servlet.methods=" + HttpConstants.METHOD_POST,
  "sling.servlet.methods=" + HttpConstants.METHOD_GET,
  "sling.servlet.resourceTypes="+ "redquark/servlets/example",
  "sling.servlet.extensions=" + "html"
 })
public class SlingAllMethodsServletExample extends SlingAllMethodsServlet {

 private static final long serialVersionUID = 7242189079618404960L;

 private final Logger log = LoggerFactory.getLogger(this.getClass());
 
 @Override
 protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
  log.info("Inside the doGet method of the SlingAllMethodsServlet type servlet");
  try {
   PrintWriter out = response.getWriter();
   out.println("Read + Write servlet got executed!!!");
  } catch (IOException e) {
   log.error(e.getMessage(), e);
  }
 }
 
 @Override
 protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
  log.info("Inside the doPost method of the SlingAllMethodsServlet type servlet");
  try {
   PrintWriter out = response.getWriter();
   out.println("Read + Write servlet got executed!!!");
  } catch (IOException e) {
   log.error(e.getMessage(), e);
  }
 }
}

Conclusion

In this blog post, we learnt about the types of Sling Servlets - SlingSafeMethodsServlet and SlingAllMethodServlet along with their code examples. You can find the complete code on my GitHub.

I hope you enjoyed this post. Your suggestions and feedback are always welcome. Feel free to befriend me on FacebookTwitter or Linked In or say Hi by email.

Happy Learning 😊

Comments

Popular posts from this blog

Day 00: AEM Developer Series

Hello everyone! Welcome to this AEM development series. We can all see that the revolution of Digital Marketing today. Companies are dying to be a part of it and they have made this a war for the Digital Marketing tools.

Adobe was way ahead in this war and have gained a lot of market capture. They are leaders in the Digital Marketing platforms since the 2014-15. One of the flagship product in its Digital Marketing suite is Adobe Experience Manager (AEM).

Since AEM is in huge demand, the people who know how to develop on AEM are also in huge demand. But developing on AEM is not easy as it is made up of various open-source technologies such as Apache Felix (OSGi), Apache Sling, Apache Oak and Adobe's own technologies like Granite, HTL etc. Learning all these technologies in conjunction can sometimes become confusing and frustrating 😫.

When I first started learning AEM in 2016, I was dumbfounded to see there is so much going on under the hood. I then spent months to gather all the …

Day 01: Introduction to AEM

Day 04: Developing first OSGi bundle