Servlets | Why a servlet registered for a page component is invoked only if called with .html as extension ? | Community
Skip to main content
VeenaVikraman
Community Advisor
Community Advisor
September 5, 2022
Solved

Servlets | Why a servlet registered for a page component is invoked only if called with .html as extension ?

  • September 5, 2022
  • 2 replies
  • 2127 views

Hi All

    

     I am trying to understand something. I am writing a servlet for a page resourceType something like below. Now as you can see, my extension is "txt". But when I call the page from my browser , if we follow the sling principles , my servlet has to be invoked when I call the page path with .txt , but it gets invoked only if I call it will .html . 

 

For e.g , 

 

 1. I created a sample project with latest archetype. 

2. In that the we have an OOTB sample servlet like below  

 

3. Now run and deploy this servlet and try to invoke this servlet. If you hit the page with http://localhost:4502/content/demo-training/us/en/test-page.txt (as per the servlet , the page component invoked using .txt extension ) ; then it will give you an error page something like below . 

 

 

When you check the request call , you can see that extension is "txt" but it is not invoking your servlet , but the default servlet 

 

 

4. Now try to hit the URL as http://localhost:4510/content/demo-training/us/en/test-page.txt.html . This will actually invoke the servlet. 

 

 

 

When you check the request call , you can see that extension is "html" and "txt" is a selector; but it is invoking your servlet .

First it calls the /libs/Page/Page.jsp to add jcr:content to the request and then actually resolves to find the servlet we registered for "txt" extension. 

URI=/content/demo-training/us/en/test-page.txt.html handled by Servlet=/libs/cq/Page/Page.jsp

 If you scroll down you will see this is then calling our servlet with below information. The request says it has "html" as extension and "txt" as selector. 

63156 LOG Including resource JcrNodeResource, type=demo-training/components/page, superType=null, path=/content/demo-training/us/en/test-page/jcr:content (SlingRequestPathInfo: path='/content/demo-training/us/en/test-page/jcr:content', selectorString='txt', extension='html', suffix='null')
  63159 TIMER_START{resolveServlet(/content/demo-training/us/en/test-page/jcr:content)}
  63995 TIMER_END{835,resolveServlet(/content/demo-training/us/en/test-page/jcr:content)} Using servlet com.xxx.training.core.servlets.SimpleServlet

 

 

 

If I register a similar servlet for a component (not page rendering component)  it works as expected .

 

What I want to understand is , why the behavior is different for only page components. It do not justify the servlet registration. If I am registering a servlet for .txt as extension , why I need to add .html before each request to invoke the servlet ?

 

Any help is appreciated

Thanks

Veena ✌

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 arunpatidar

Call it using http://localhost:4502/content/demo-training/us/en/test-page/_jcr_content.txt 

You will have your answer 😉

 

or register the servlet using resourcTypes=cq/Page then call it using  http://localhost:4502/content/demo-training/us/en/test-page.txt

2 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
September 5, 2022

Call it using http://localhost:4502/content/demo-training/us/en/test-page/_jcr_content.txt 

You will have your answer 😉

 

or register the servlet using resourcTypes=cq/Page then call it using  http://localhost:4502/content/demo-training/us/en/test-page.txt

Arun Patidar
Adobe Employee
September 14, 2022

@arunpatidar @veenavikraman 

If we use resourceTypes = cq/Page, then servlet will be called using any page path right? its not specific to page component.  is my understanding right?

arunpatidar
Community Advisor
Community Advisor
September 14, 2022

Hi @amsalek4 
Yes, the servlet will be called for all the nodes of cq:Page type.

Arun Patidar
joerghoh
Adobe Employee
Adobe Employee
September 5, 2022

@arunpatidar  is correct. I explained the details why this is working in exactly that way in https://cqdump.joerghoh.de/2019/01/07/how-does-sling-resolve-an-aem-page-to-the-correct-resource-type/

VeenaVikraman
Community Advisor
Community Advisor
September 6, 2022

Yeah @joerghoh I have referred that post and it makes sense now. Thanks @arunpatidar and @joerghoh