0

So I have 2 servlets annotated like this:

@Component
@Service(value = {Servlet.class, NonExistingResourceServlet.class})
@Properties({
    @Property(name = "sling.servlet.resourceTypes",
            value = {"sling/servlet/default"},
            propertyPrivate = true),
    @Property(name = "sling.servlet.extensions",
            value = {"xml"},
            propertyPrivate = true),
    @Property(name = "sling.servlet.methods",
            value = {"GET"},
            propertyPrivate = true))

For both I override the accepts method

@Override
public boolean accepts(SlingHttpServletRequest request) {
    String requestURI = request.getRequestURI();
    if (StringUtils.isNotBlank(requestURI)){
        return requestURI.endsWith("/sevlet1.xml"); //different for the other servlet
    } else {
        return false;
    }
}

IN CQ /system/console/servletresolver one of them is not resolved. Do I have to be more specific in configuration. The accepts method is not enough? Found on Apache Sling doc

If a registered servlet implements the OptingServlet interface, Sling uses that servlet's accepts(SlingHttpServletRequest request) method to refine the servlet resolution process. In this case, the servlet is only selected for processing the current request if its accept method returns true.

For one of them I added a selector and now the difference is made. My question is why do I need to add the selector if I override the accepts method?

The 2 servlets are like this: /content/myapp/sevlet1.xml /content/myapp/sevlet2.xml

Community
  • 1
  • 1
Bogdan
  • 412
  • 1
  • 3
  • 14
  • I never tried the OptingServlet approach, but the other option would be a ServletFilter: http://sling.apache.org/documentation/the-sling-engine/filters.html Do you really need to have a fixed url ending? In my experience the best way to distinguish two sling servlets is with the use of selectors: http://sling.apache.org/documentation/the-sling-engine/url-decomposition.html – Thomas Feb 05 '15 at 08:31

1 Answers1

1

I'm not sure whether the /system/console/servletresolver tool takes OptingServlet into account, you might also put a debugger breakpoint in your accept methods to check that they are actually called when the corresponding HTTP requests come in. Note also that your servlets need to be declared "implements OptingServlet", which I suppose is the case as you have an @Override annotation on your accepts method.

Note that an OptingServlet that checks the request URI is usually not recommended in Sling, dispatching based on resource types or selectors is the recommended best practice.

(edit:) You might also compare your code with the example OptingServlet from the Sling integration tests.

Bertrand Delacretaz
  • 6,100
  • 19
  • 24
  • hi can you answer this question http://stackoverflow.com/questions/28361998/how-to-provide-custom-value-on-checkbox-in-cq5-dialog – user2142786 Feb 06 '15 at 09:52
  • Tried with "implements OptingServlet" but no success. Using the /system/console/servletresolver found that both requests are resolved to the same and only servlet. Using selectors or resource types is the way to go. – Bogdan Feb 09 '15 at 15:17
  • I knew that but why is there the option if it is not working? I wonder if I could set something up to make it work. – Bogdan Feb 09 '15 at 15:18
  • I have added a link to an OptingServlet example to this answer. As mentioned, you could also set breakpoints in your servlet's accept methods to check what actually happens. – Bertrand Delacretaz Feb 16 '15 at 12:24