Expand my Community achievements bar.

SOLVED

Approach for creating Sling Servlet

Avatar

Level 4

Hi All,

 

What is the better approach to create a Servelet.

ResoureType or Path Based.

 

As per documentation it is ResourceType only.

 

But sometimes it happens that it exposes the internal Structure of AEM may be Content Structure or Component Structure.

It is ok to expose the internal Structure of AEM for creating a Servlet.

 

Any recommendations for this.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Sb2512 , You can make use of the proxy component pattern to avoid exposing the exact component location within AEM repo while using a resource type bound servlet.

Although, this is not the primary use case of using the proxy component pattern but it does give you this ability as well.

 

You can refer to the below documentation links to under stand how this works : 

 

https://experienceleague.adobe.com/docs/experience-manager-core-components/using/developing/guidelin...

https://experienceleague.adobe.com/docs/experience-manager-core-components/using/get-started/using.h...

 

View solution in original post

4 Replies

Avatar

Community Advisor

Hi @Sb2512 

As per sling documentation its recommended to go for resource type because of the following major reasons 

 

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

Caveats when binding servlets by path

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.

 

In the specific case you highlighted we can control the resource bound one with ACLs and can prevent any potential misuse here 

Avatar

Correct answer by
Community Advisor

@Sb2512 , You can make use of the proxy component pattern to avoid exposing the exact component location within AEM repo while using a resource type bound servlet.

Although, this is not the primary use case of using the proxy component pattern but it does give you this ability as well.

 

You can refer to the below documentation links to under stand how this works : 

 

https://experienceleague.adobe.com/docs/experience-manager-core-components/using/developing/guidelin...

https://experienceleague.adobe.com/docs/experience-manager-core-components/using/get-started/using.h...

 

Avatar

Level 5

If you are building apis that accept different parameters or may be upload some to process before responding back you can comfortably use path based servlet. Sling doesn't discourage path based servlets all together. But suggests usage of resource type based servlets when you intend to perform actions specific to a resource in context as it helps controlling the access based on the resource etc. Follow the links shared in other replies from our community members.