Highlighted

AEM 6.3 - registering Servlets with paths

Avatar

Avatar

brendanf9753525

Avatar

brendanf9753525

brendanf9753525

10-04-2018

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.

Replies

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

10-04-2018

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?

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

10-04-2018

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 {

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

10-04-2018

Highlighted

Avatar

Avatar

Jineet_Vora

Avatar

Jineet_Vora

Jineet_Vora

02-12-2019

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.

Highlighted

Avatar

Avatar

BrianKasingli

MVP

Avatar

BrianKasingli

MVP

BrianKasingli
MVP

02-12-2019

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!