Multiple Sling Opting Servlets,

27 03 2011

Sling OptingServlets allow a servlet to be registered to a resource type and decide if its going to handle the resource. This sounds good, except that there is only one slot available per registration. If you have a resource type X, method GET and you want to have 5 Servlets registered to optionaly process requests against resource type X with no selectors and no extensions, then you cant do it.

The solution is to have a single Servlet registered as an Opting servlet per resource type, and have it manage a set of Servlet implementations that opt on that single registration. The only thing that you need to be careful about is to create a service interface that Sling wont pickup and register as a servlet, otherwise the likelihood is it will overwrite your Servlet multiplexer, and with 2 identical registrations of the same request/resource patter, its random which one will be active.



2 responses

28 03 2011
Felix Meschberger

You still should be able to use the javax.servlet.Servlet interface as the service interface. Just make sure to not use the same service registration properties as used by Sling.

28 03 2011

Yes, Servlet works, however since the Sling servlets all take SlingHttpServletRequest and SlingHttpServletResponse I found, the following worked well. The init() and destroy() methods on the HttpServlet have to be called by the switch to ensure that delegates are initialised/destroyed correctly. The reason for the doDelegate is that doGet is not part of the Servlet interface, its part of the HttpServlet abstract class.

public interface DeligatedServlet extends OptingServlet {

public void doDelegateGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException;


%d bloggers like this: