Expand my Community achievements bar.

SOLVED

Trying to run Servlet when Selector Exists for any page

Avatar

Level 7

I am trying to create a servlet that targets all AEM pages, and activates only when the selector exists, why is this not working?

 

/content/my-site/en.testing.html, is not working...

 

(service = Servlet.class, property = {
        ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES + "=sling/servlet/default",
        ServletResolverConstants.SLING_SERVLET_SELECTORS + "=testing",
        ServletResolverConstants.SLING_SERVLET_METHODS + "=GET",
        ServletResolverConstants.SLING_SERVLET_EXTENSIONS + "=html"})

public class TestGetServlet extends SlingSafeMethodsServlet {

    
    protected void doGet(@NotNull final SlingHttpServletRequest request,  final SlingHttpServletResponse response) throws IOException {
        System.out.println("testing");
    }
}

 

 

This also does not work

@SlingServletResourceTypes(resourceTypes = "sling/servlet/default",
        methods = HttpConstants.METHOD_GET,
        selectors = "testing")
public class TestGetServlet extends SlingSafeMethodsServlet {

    @Override
    protected void doGet(@NotNull final SlingHttpServletRequest request, @notnull final SlingHttpServletResponse response) throws IOException {
        System.out.println("testing");
    }
}

 

 

 

Can you help? thanks!

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi @SupportMember ,
First of all, you have to use OSGi DS 1.4 (R7) annotations. These annotations are recommended. So use your second syntax. If you overwrite default servlet, your servlet will be called. 
In your servlet does not has extensions and you are calling with extension(.html). Add extensions (html). 
these methods, selectors and 
extensions are array type. So it is recommended to add values in Array. There is one more catch here.  If you don't define extensions but define selectors, your selectors can be use with extensions. means you can call only with selector(/content/my-site/en.testing). But it is not recommended. Define both selectors and extensions.
Adding a sample servlet.

@Component(service = Servlet.class)
@SlingServletResourceTypes(
        methods = {HttpConstants.METHOD_GET,HttpConstants.METHOD_POST},
        resourceTypes = "sling/servlet/default",
        selectors = {"geeks"},
        extensions = {"html"}
)
public class DefaultServlet extends SlingAllMethodsServlet {
    private static final Logger LOG = LoggerFactory.getLogger(DefaultServlet.class);
    @Override
    protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/plain");
        resp.getWriter().write("This is Default Servlet GET method ");
    }
    @Override
    protected void doPost(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/plain");
        resp.getWriter().write("This is Default Servlet POST method ");
    }

 

 call with path/your-page.geeks.html

View solution in original post

3 Replies

Avatar

Community Advisor

If you the sling documentation for servlets, resolution happens with service ranking as well

Please give your servlet with highest ranking in order to pick the default servlet

https://sling.apache.org/documentation/the-sling-engine/servlets.html

 

Hope this solves issue

Avatar

Correct answer by
Level 7

Hi @SupportMember ,
First of all, you have to use OSGi DS 1.4 (R7) annotations. These annotations are recommended. So use your second syntax. If you overwrite default servlet, your servlet will be called. 
In your servlet does not has extensions and you are calling with extension(.html). Add extensions (html). 
these methods, selectors and 
extensions are array type. So it is recommended to add values in Array. There is one more catch here.  If you don't define extensions but define selectors, your selectors can be use with extensions. means you can call only with selector(/content/my-site/en.testing). But it is not recommended. Define both selectors and extensions.
Adding a sample servlet.

@Component(service = Servlet.class)
@SlingServletResourceTypes(
        methods = {HttpConstants.METHOD_GET,HttpConstants.METHOD_POST},
        resourceTypes = "sling/servlet/default",
        selectors = {"geeks"},
        extensions = {"html"}
)
public class DefaultServlet extends SlingAllMethodsServlet {
    private static final Logger LOG = LoggerFactory.getLogger(DefaultServlet.class);
    @Override
    protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/plain");
        resp.getWriter().write("This is Default Servlet GET method ");
    }
    @Override
    protected void doPost(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/plain");
        resp.getWriter().write("This is Default Servlet POST method ");
    }

 

 call with path/your-page.geeks.html

Avatar

Community Advisor

you can registered your servlet using below to resolved servlet for all the pages when called with selector 'testing'

@Component(
        service = Servlet.class,
        property = {
                Constants.SERVICE_DESCRIPTION + "=My servlet description",
                "sling.servlet.methods=" + HttpConstants.METHOD_GET,
                "sling.servlet.selectors=" + "testing",
                "sling.servlet.resourceTypes=" + "cq:Page",
                "sling.servlet.extensions=" + "html"
        }
)

 



Arun Patidar