why do sling servlets only work with with a path of /bin/demo/xxx? | Community
Skip to main content
Level 8
April 6, 2021
Solved

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

  • April 6, 2021
  • 2 replies
  • 3663 views

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?
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 Asutosh_Jena_

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:

 

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!

2 replies

Asutosh_Jena_
Community Advisor
Asutosh_Jena_Community AdvisorAccepted solution
Community Advisor
April 6, 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:

 

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!

TB3dockAuthor
Level 8
April 6, 2021
Thanks for the comprehensive answer. I tried the new type of sling servlets, but could not get them to work. The old ones are good in that you can just create the java file and they work - no magic sauce required (other than choosing a path which starts with /bin!)
Pawan-Gupta
Level 8
April 6, 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!!