Call servlet post request inside ResourceChangeListener in aem | Community
Skip to main content
Level 2
April 16, 2024
Solved

Call servlet post request inside ResourceChangeListener in aem

  • April 16, 2024
  • 3 replies
  • 2084 views

I wrote Resource change Listener and it will call if i save my dialog with any modification, Now i want to call a servlet (POST request ) inside the listener and the servlet call should be success ,And any example would be helpful.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by aanchal-sikka

I am trying to access resource like below with resource reoslver
try
(ResourceResolver serviceResourceResolver = resourceResolverFactory.getServiceResourceResolver(null)) {

Resource resource = serviceResourceResolver.resolve(path);

}

i try to access resource.getPath() , getting the correct path, with the tag as

"NonExistingResource"

What will be the reason


@yugansan 

 

Requesting you to please go through the video that I had shared earlier. There would be a lot of blogs also available.

 

This code isn't right

resourceResolverFactory.getServiceResourceResolver(null)) {

 

As service use mapping needs to exist against the bundle/Service with proper privileges.

3 replies

aanchal-sikka
Community Advisor
Community Advisor
April 16, 2024

@yugansan 

 

  • Ideally one should create a Service and use it in the Listener.
    • Extract the required code in a common abstract class, which can be used by both Servlet and Service.
  • Do not execute heavy operations in listeners. Use Sling jobs instead 
Aanchal Sikka
RikVanB
Level 2
April 16, 2024

Hi @yugansan 
It's indeed like @aanchal-sikka is stating, the idea should be that your Servlet is using an underlying service which includes the logic that needs to be executed when calling your Servlet. If that is the case you could reuse the same service from your servlet into your ResourceChangeListener.

Greetings

Rik

YuganSanAuthor
Level 2
April 16, 2024

How we can call our service inside onchange method

myservlet.doSomething() (dosomething method i wrote inside the servelt as we cant able to call dopost method)

YuganSanAuthor
Level 2
April 23, 2024

@yugansan 

 

You would need to user service-user to access resources. The Service itself doesn't have a session. 

https://www.youtube.com/watch?v=ZaV-JvgbFTc


I am trying to access resource like below with resource reoslver
try
(ResourceResolver serviceResourceResolver = resourceResolverFactory.getServiceResourceResolver(null)) {

Resource resource = serviceResourceResolver.resolve(path);

}

i try to access resource.getPath() , getting the correct path, with the tag as

"NonExistingResource"

What will be the reason

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
April 18, 2024

@yugansan Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.

Esteban Bustamante