How to not cache response of default servlet | Community
Skip to main content
Level 2
February 13, 2024
Solved

How to not cache response of default servlet

  • February 13, 2024
  • 4 replies
  • 1536 views

@8220494(immediate = true, service = Servlet.class, property={
"sling.servlet.methods=" + "GET",
"sling.servlet.resourceTypes=" + "sling/servlet/default",
"sling.servlet.extensions=" + "abc"
})
public class abcServlet extends SlingAllMethodsServlet {

}

 

we have above default servlet with extension.
we dont want to cache the response from API when this is triggered.

 

we are trying to add this rule in dispatcher rules.any file.

#  Rule. Don't cache API requests
/0030 {
/glob "/*.abc.html"
/type "deny"
}

will this be sufficient or do we have any alternate way to do this job?

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 Jagadeesh_Prakash

@sudarshanreddy  if u are looking for alternative way other than dispatcher try setting below response header to your servlet

 

response.setHeader("Dispatcher", "no-cache");

 

 

@Inject
 private SlingHttpServletResponse response;

 @PostConstruct
 protected void init() {
     response.setHeader("Dispatcher", "no-cache");
 }

 

4 replies

Imran Khan
Community Advisor
Community Advisor
February 13, 2024

Please follow below URL for more details

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-cache-pages-with-selector-in-url/m-p/435085

/0405 { /glob "*.print.html/*" /type "deny" }


print is selector here

Level 2
February 13, 2024

Im looking for alternative way to do?

Jagadeesh_Prakash
Community Advisor
Jagadeesh_PrakashCommunity AdvisorAccepted solution
Community Advisor
February 13, 2024

@sudarshanreddy  if u are looking for alternative way other than dispatcher try setting below response header to your servlet

 

response.setHeader("Dispatcher", "no-cache");

 

 

@Inject
 private SlingHttpServletResponse response;

 @PostConstruct
 protected void init() {
     response.setHeader("Dispatcher", "no-cache");
 }

 

Jagadeesh_Prakash
Community Advisor
Community Advisor
February 13, 2024

Hi @sudarshanreddy  Inside URL params. Try below code

 

/ignoreUrlParams
{
#/0001 { /glob "*" /type "deny" }

/0002 { /glob "abc" /type "deny" }

}
arunpatidar
Community Advisor
Community Advisor
February 13, 2024

Hi @sudarshanreddy 
Use abc selector to register the Servlet and deny all url from that selector.

Arun Patidar
TarunKumar
Community Advisor
Community Advisor
February 13, 2024

Hi @sudarshanreddy ,

You can set response header in your servlet that will prevent it to be cached.
Something like below:

response.setHeader("Dispatcher", "no-cache");

 

Thanks
Tarun