Expand my Community achievements bar.

AEM 6.3 - registering Servlets with paths

Avatar

Level 4

Hi all,

I have a servlet that I am trying to register at the /feed path. It will be a servlet that takes GET requests and returns out some data.

My servlet has this path at the top when registering it.

@SlingServlet(paths = "/feed", methods = "GET", metatype = true)

I also went into the Apache Sling Servlet/Script Resolver and Error Handler and added the /feed/ path as one of the allowed scripts.

1462510_pastedImage_0.png

However, whenever I visit localhost:4502/feed, I get a 403 forbidden error. I see that it gets to my class and path, but it throws a 403 regardless.

However, If I move the servlet to /bin/feed, I can visit it successfully without a 403 error.

Is there a way to have servlets registered at paths without having the /bin/ path in front of them? I am pretty new to java servlets so any advice/suggestions would definitely go a long way.

Thanks

Brendan.

5 Replies

Avatar

Level 10

I personally have only used syntax like:

@SlingServlet(paths="/bin/mySearchServlet", methods = "POST", metatype=true)

public class HandleClaim extends org.apache.sling.api.servlets.SlingAllMethodsServlet {

Why is using /bin an issue?

Avatar

Level 10

Also - keep in mind - if you are using new DS OSGi annotations - then the syntax is

@Component(service=Servlet.class,

        property={

                Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

                "sling.servlet.methods=" + HttpConstants.METHOD_POST,

                "sling.servlet.paths="+ "/bin/myDataSourcePoolServlet"

           })

public class SimpleServlet extends SlingAllMethodsServlet {

Avatar

Community Advisor

Per Apache Sling, there are different ways to register a servlet path.
If you want at first level then you have to register a servlet as '/feed.servlet' and then in 'Apache Sling Servlet/Script Resolver and Error Handler' configuration add '/feed.servlet' as your servlet execution path. Note that '.servlet' is expected and mandatory for first level servlet.
If you want to keep at second/more than one level then in the configuration, you have to specify the sub-tree like '/feed/' which will expect a servlet at any of the /feed/ sub-tree e.g. '/feed/fetchData' or '/feed/postData'


If you have given path as '/feed' or '/feed/' in configuration and calling the servlet with http://localhost:4502/feed , it won't work because Apache Sling expects a resource in this case and not a servlet execution script. A servlet script will only be executed in above two scenarios I mentioned.

Avatar

Community Advisor

Hello Brendan,

It seems like you want to add some sugar coat your Sling Servlet Resolver script/path. Please check out this article where it will provide a solution on how you can write endpoints/paths in a more controlled way. It will reveal to you a strategy to hide all the extensions, selectors, paths, etc by doing this the Apache Rewriter Flag.

https://sourcedcode.com/sugar-coating-servlet-scripts-and-paths

I hope this helps!