why do sling servlets only work with with a path of /bin/demo/xxx?

Avatar

Avatar

TB3dock

Avatar

TB3dock

TB3dock

06-04-2021

I copied a sling servlet which had the path of /bing/demo/querybuilder

 

@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=Query Builder servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/demo/querybuilder" })
 
In creating our own sling servlets, we tried to change the path (e.g. to "/api/xxx/yy"), but no path other than /bin/demo/ seems to work. What is magical about this path?

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

asutosh_j3

Avatar

asutosh_j3

asutosh_j3

06-04-2021

Hi @TB3dock 

 

There is an OSGi Configuration named Apache Sling Servlet/Script Resolver and Error Handler in the felix console.

It has a property called "Execution Paths(servletresolver.paths)" which holds a list of absolute paths under which the servlet is accessible as a Resource.

By default bin is allowed in the resolver path which is why it works.

 

If you want your servlet with /api path to be accessible you will need to allow the path in the resolver paths. Please see the screenshot below:

 

asutosh_j3_0-1617731121541.png

It is recommended to use resource type based servlet over path based now. Please refer the link below:

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

 

Hope this helps!

Thanks!

Answers (1)

Answers (1)

Avatar

Avatar

rush_pawan

Avatar

rush_pawan

rush_pawan

06-04-2021

hello, it is mainly for security and processing of request correctly, as i noticed. but apart from /bin you can also implement your servlet using resource type and selector like below

 

@SlingServlet(resourceTypes = "site/components/mypage",
selectors = "myselector",
extensions = "html",
methods = "GET",
metatype =true)

 

Thanks!!