Expand my Community achievements bar.

Adobe Summit 2025: AEM Session Recordings Are Live! Missed a session or want to revisit your favorites? Watch the latest recordings now.
SOLVED

Getting resourceResolver fromfactory and request

Avatar

Level 2

HI Team,

 

Getting resolver from resourceresolverfactory and getting resolver request in servlet for both by path servlet and by resourcetype servlet  what's the difference in these two.

 

Thanks,

Keerthi K.

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Ns334 

Getting ResourceResolver from ResourceResolverFactory

Permissions: Requires a system user (data-reader in this example) configured in Apache Sling Service User Mapper.

Need to close resourceResolver explicitly once done with task.

 

Getting ResourceResolver from SlingHttpServletRequest

Permissions: Uses the current logged-in user’s credentials (subject to ACL restrictions).

No need to close resourceResolver, will be taken care by request.

 

Always use resourceResolver from request without opening new one until there are specific use case/need.

Arun Patidar

AEM LinksLinkedIn

View solution in original post

3 Replies

Avatar

Community Advisor

Hi @Ns334 ,

In AEM, when using a servlet, you can obtain a ResourceResolver in two ways:

     - Using ResourceResolverFactory
     - Using SlingHttpServletRequest#getResourceResolver()

1. Getting ResourceResolver from ResourceResolverFactory

This is a generic way to obtain a ResourceResolver, independent of the request.

Applicable for both Path-based and ResourceType-based servlets.

@Reference
private ResourceResolverFactory resourceResolverFactory;

public ResourceResolver getResourceResolver() {
    Map<String, Object> authInfo = Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, "data-reader");
    try {
        return resourceResolverFactory.getServiceResourceResolver(authInfo);
    } catch (LoginException e) {
        throw new RuntimeException("Failed to get ResourceResolver", e);
    }
}

Use Case: When you need a ResourceResolver for background jobs, services, or when no request object is available.
Permissions: Requires a system user (data-reader in this example) configured in Apache Sling Service User Mapper.

2. Getting ResourceResolver from SlingHttpServletRequest

This method derives the ResourceResolver from the incoming request.
Only applicable within request-based scenarios (Servlets, Models, Filters).

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    ResourceResolver resolver = request.getResourceResolver();
    Resource resource = resolver.getResource(request.getRequestPathInfo().getResourcePath());
    response.getWriter().write("Resource Path: " + resource.getPath());
}

Use Case: When handling user-specific requests that rely on the session’s authentication.
Permissions: Uses the current logged-in user’s credentials (subject to ACL restrictions).

When to Use Which?

1. System service requiring elevated permissions → Use ResourceResolverFactory (System User).
2. Request-based logic (retrieving user’s session resolver) → Use request.getResourceResolver().
3. API endpoint that doesn’t depend on a resource → Use Path-Based Servlet.
4. Component-specific processing (e.g., fetching data for a dialog) → Use ResourceType-Based Servlet.

Regards,
Amit Vishwakarma

Avatar

Correct answer by
Community Advisor

Hi @Ns334 

Getting ResourceResolver from ResourceResolverFactory

Permissions: Requires a system user (data-reader in this example) configured in Apache Sling Service User Mapper.

Need to close resourceResolver explicitly once done with task.

 

Getting ResourceResolver from SlingHttpServletRequest

Permissions: Uses the current logged-in user’s credentials (subject to ACL restrictions).

No need to close resourceResolver, will be taken care by request.

 

Always use resourceResolver from request without opening new one until there are specific use case/need.

Arun Patidar

AEM LinksLinkedIn

Avatar

Level 2

Thank you.