Search code examples
servletsaemsling

Why sling.servlet.prefix in Sling Servlets?


I am trying to use sling.servlet.prefix option in Sling Servlets. As per my understanding if I put a relative path to resourceType and add a prefix to /apps or /libs it will directly point to that path. But it is not happening.

> @Component(service=Servlet.class,
>         property={
>                 Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
>                 "sling.servlet.methods=" + HttpConstants.METHOD_GET,
>                 "sling.servlet.resourceTypes="+ "community-components/components/componentpage",
>                 "sling.servlet.extensions=" + "sample",
>                 "sling.servlet.prefix=" + "/apps"
>         })

Please help.


Solution

  • The value of the sling.servlet.prefix needs to start with a / for its value to be applied as the prefix. Else, it expects a integer or a String that can be parsed as an integer which would be the index of the search path entries from the resource resolver to be used as the prefix.

    Since your prefix didn't start with a /, it would try to convert it into a integer and since it is not a valid integer it would be ignored.

    In your case the prefix would be /apps/.

    More information on prefix can be found here.