Trying to run Servlet when Selector Exists for any page | Community
Skip to main content
Level 6
January 7, 2023
Solved

Trying to run Servlet when Selector Exists for any page

  • January 7, 2023
  • 3 replies
  • 1494 views

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 { @9944223 protected void doGet(@NotNull final SlingHttpServletRequest request, @126844 final SlingHttpServletResponse response) throws IOException { System.out.println("testing"); } }

 

 

 

Can you help? thanks!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by sunil_kumar_

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

3 replies

Mani_kumar_
Community Advisor
Community Advisor
January 7, 2023

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

sunil_kumar_
sunil_kumar_Accepted solution
Level 5
January 8, 2023

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

arunpatidar
Community Advisor
Community Advisor
January 8, 2023

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