Expand my Community achievements bar.

sling servlet path

Avatar

Level 3

while registering the servlet using path can i use the .* 

example

@Component(service = Servlet.class, property = { 
        "sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/test",
"sling.servlet.paths=" + "/bin/test/.*",})
 
When i am trying to hit http://localhost:4502/bin/test it is working fine but when i am accessing http://localhost:4502/bin/test/servlet it is redirecting to error page
5 Replies

Avatar

Community Advisor

Hi @HelpTech 

 

1. Can you check this thread if its relevant? -Re: @SlingServletPaths, is there a way to specify ... - Adobe Experience League Community - 577553  It seems /bin/test should cater to /bin/test and /bin/test/abc 

 

2. Otherwise, have you tried like below just to make sure?

 

@Component(service = Servlet.class, property = {
    "sling.servlet.methods=" + HttpConstants.METHOD_GET,
    "sling.servlet.paths="/bin/test",
    "sling.servlet.paths="/bin/test/*"
})

  Based on Apache Sling :: Servlets and Scripts it does not seem like there is a wild character or pattern is allowed.

 

3. Just curious what is the use case to use servlet with varying paths? Trying to see if using sling filter pattern could suffice to the use case or requirement.

Avatar

Community Advisor

Hi @HelpTech , No, Sling doesn't support wildcards in path.

It is mentioned in the sling documentation as well, that the path attribute accepts only absolute paths. The usage of path based servlets is also discouraged, and it is suggested to use resource based servlets, which provide better security.

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

Fanindra_Surat_0-1733100321261.png

 

For your use case - try using request selectors or path suffixes to add some dynamic elements to the URL.

 

- Regards,

Avatar

Community Advisor

Hi @HelpTech ,

It doesn't support wild character or pattern however you may provide array of path if it is limited an know.

Another approach can be create a filter which support pattern and from there redirect(call) your servlet.

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/dynamically-redirect-to-ap...

Thanks

Avatar

Level 2

Hi @HelpTech 

 

1. First of all I believe your annotation has an error. You can't have multiple sling.servlet.paths props as far as I know.. So try having only one, and define it based on the specs:

Sorin_Diaconesc_0-1733123985356.png

 

2. Second, specs are not saying we can use wildcards. Which means you have you use other options if you want your servlet to respond to multiple paths. Options already have been mentioned, like URL selectors or extensions. If you wanna go with this approach, please check what sling.servlet.paths.strict property is doing. You might need it, based on you use cases.

Sorin_Diaconesc_1-1733124304977.png

 

3. As it has been already advised here, best practice is to use resource based servlets. Also the specs points out this:

Sorin_Diaconesc_2-1733124328626.png



Few useful articles out there on AEM servlets:

 

Avatar

Community Advisor

@HelpTech : What is your exact use-case? Do you want this servlet to trigger on multiple paths. If those are your content page paths, you can consider registering your servlet for resource types instead of paths.

You can refer this documentation in-case to see how to register with resource type:-

OR, 
You can specify one path such as "/bin/test" and for other variations specify them as selectors and pass those values while invoking the servlet. This is with assumptions that there are not too many such variations (for too many it doesn't make sense and you will you have reconsider your use-case for using paths for registering servlet).
sling.servlet.paths =  "/bin/test"
sling.servlet.selectors = [ "selector-1, selector-2" ]