April 10, 2020
Estimated Post Reading Time ~

Sling Servlets 05: Types of 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 override.

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 POSTPUT 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 SlingSafeMethodsServletExample extends SlingSafeMethodsServlet {

 private static final long serialVersionUID = 8565932343267006374 L;

 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 POSTPUT 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 learned about the types of Sling Servlets - SlingSafeMethodsServlet and SlingAllMethodServlet along with their code examples. You can find the complete code on my GitHub.


By aem4beginner

No comments:

Post a Comment

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