Expand my Community achievements bar.

SOLVED

Passing content path to a Servlet through selectors fails -Need Suggestion

Avatar

Level 4

Hi All,

I have a requirement to build a latest article component which can be used across the many other site.

So I just thought of  creating a servlet which will query all the article under a path(/content/geo/article) which can be controlled by author and send me a JSON response.

Considering the caching startegy I need to pass the parameters as selectors .(Passing as query parameter will not allow me to cache the json until some new article is created)

For Example /bin.article.<limit>.<offset>.json .

Now I need to pass the pagepath lets say  /content/geo/article to servlet .Am not able to pass it as selector.Can anyone suggest me a solution for this.

Thanks,
Sambasivaraja.

1 Accepted Solution

Avatar

Correct answer by
Level 9

Hi Sameer,

In such case, we should not register our servlet with fixed path. Map your servlet with selector & resource Type (if resource is one for all article pages). And, when you are trying to call servlet, you currentPath.<selectors>.json as URL to invoke servlet.

Here is the code snippet.

@SlingServlet(resourceTypes = "/apps/page/articlePage",selectors = "article",extensions = "html",methods = "GET")public class DummyServlet extends SlingSafeMethodsServlet {@Overrideprotected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {...}}

URL would.. /content/mysite/pagearticle.article.limit.offset.html.

This is how you can invoke your servlet and pass current page path along with other parameters. Servlet would be resolved through selectors & resource type.

View solution in original post

2 Replies

Avatar

Correct answer by
Level 9

Hi Sameer,

In such case, we should not register our servlet with fixed path. Map your servlet with selector & resource Type (if resource is one for all article pages). And, when you are trying to call servlet, you currentPath.<selectors>.json as URL to invoke servlet.

Here is the code snippet.

@SlingServlet(resourceTypes = "/apps/page/articlePage",selectors = "article",extensions = "html",methods = "GET")public class DummyServlet extends SlingSafeMethodsServlet {@Overrideprotected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {...}}

URL would.. /content/mysite/pagearticle.article.limit.offset.html.

This is how you can invoke your servlet and pass current page path along with other parameters. Servlet would be resolved through selectors & resource type.

Avatar

Level 1

Hello Sameer

We shouldn't register our servlet with a set path in this situation. If the resource is a single resource for all article pages, map your servlet with a selector and resource Type. In addition, you use currentPath.selectors> when attempting to invoke a servlet.JSON as the URL for the servlet call.

This is the piece of code.

@SlingServlet(resourceTypes = "/apps/page/articlePage", selectors = "article", extensions = "html", methods = "GET")public class DummyServlet extends SlingSafeMethodsServlet. @Overrideprotected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException...

This is how you can call your servlet and pass other parameters along with the current page's path. Selectors & resource type would be used to resolve the servlet.