Expand my Community achievements bar.

SOLVED

SlingSafeMethodsServlet: Why use this over HttpServlet?

Avatar

Level 3

Good day all. Some help understanding the above will be really appreciated.

I have a class that extends HttpServlet instead of SlingSafeMethodsServlet. I use @SlingServlet to expose a URL endpoint.

@SlingServlet(paths = "/apps/myapp") MyServlet extends HttpServlet { ... }

Everything works as expected of a servlet.

Looking at the source for SlingSafeMethodsServlet there aren't any obvious differences between SlingSafeMethodsServlet and HttpServlet. In fact the implementation of methods in SlingSafeMethodsServlet and SlingAllMethodsServlet are simply calls to HttpServlet methods. Have I not looked closely enough?

What are the concrete cases/examples of when you'd want to or have to extend SlingSafeMethodsServlet/SlingAllMethodsServlet instead of HttpServlet?

 

Edit: From SlingSafeMethodsServlet source

/** * Helper base class for read-only Servlets used in Sling. This base class is * actually just a better implementation of the Servlet API <em>HttpServlet</em> * class which accounts for extensibility. So extensions of this class have * great control over what methods to overwrite. * **/

What does this mean exactly? Simply that it only allows for read methods "doGet" (write operations are to be found in SlingAllMethodsServlet)? Is that the entire extent of the difference between SlingSafeMethodsServlet and HttpServlet?

1 Accepted Solution

Avatar

Correct answer by
Level 10
1 Reply

Avatar

Correct answer by
Level 10

The best answer is its a convention when using Sling. Refer to the Javadocs:

https://sling.apache.org/apidocs/sling5/org/apache/sling/api/servlets/SlingSafeMethodsServlet.html