Expand my Community achievements bar.

SOLVED

How to not cache response of default servlet

Avatar

Level 2

@component(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?

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@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");
 }

 

View solution in original post

7 Replies

Avatar

Community Advisor

Please follow below URL for more details

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-cache-pages-with-se...

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


print is selector here

Avatar

Correct answer by
Community Advisor

@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");
 }

 

Avatar

Community Advisor

Hi @sudarshanreddy  Inside URL params. Try below code

 

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

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

}

Avatar

Community Advisor

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



Arun Patidar

Avatar

Community Advisor

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