Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.

When we go for Sling Servlet registered by Path, Resource Type?

Avatar

Level 2

Hi All

           I'm new to this Sling Servlets, trying to registering a servlet by path, as well as registering a Servlet by Resource Type that is fine.. when we go for Path and When we go for a Resource Type. give me a explanation here

4 Replies

Avatar

Employee Advisor

By default you should register Servlets via Resource Types. I don't know reasons to bypass this rule.

Jörg

Avatar

Level 8

Binding servlets by paths has several disadvantages when compared to binding by resource types, namely:

  • path-bound servlets cannot be access controlled using the default JCR repository ACLs
  • path-bound servlets can only be registered to a path and not a resource type (i.e. no suffix handling)
  • if a path-bound servlet is not active, e.g. if the bundle is missing or not started, a POST might result in unexpected results. usually creating a node at /bin/xyz which subsequently overlays the servlets path binding
  • the mapping is not transparent to a developer looking just at the repository

Given these drawbacks it is strongly recommended to bind servlets to resource types rather than paths.

Avatar

Level 10

Binding by path is useful when you want a non-resource to post data to it. For example, a Java client app that posts data to an AEM Sling Servlet. Say the path is  /bin/upfile.

String aemPostUrl = host+"/bin/upfile";

The above string would represent the URL to post to.

Here are the supported ways to register a servelt.

Apache Sling :: Servlets and Scripts

As you can see - both Resource types and Paths are supported. 

Avatar

Employee Advisor

Sorry Scott,

but even in this case it makes sense to bind the servlet to a resource instead of a path. Because as already stated by cquser1​ you can apply ACLs on that; that means that you control if a certain user has the ability to post at all for free (that means with no coding) and configurable during runtime. You need a lot of coding if you want to implement this logic in your servlet :-)

Jörg