Expand my Community achievements bar.

SOLVED

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

Avatar

Level 9

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?
1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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!

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

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!

Avatar

Level 9
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!)

Avatar

Level 9
One thing I dont get in AEM. the majority of tutorials and articles only tell you how to create critical items in the UI of a running local aem Dev instance, including page template types, page templates, components and now servlet paths. Adding a new path on my local AEM instance is rather pointless, as it wont get build and deployed from Git to the cloud servers. So Ill stick with /bin.

Avatar

Level 9

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!!