Expand my Community achievements bar.

SOLVED

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

Avatar

Community Advisor

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  

Veena_Vikram_0-1662372823275.png

 

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 . 

Veena_Vikram_1-1662372941227.png

 

 

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

Veena_Vikram_0-1662380135055.png

 

 

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. 

 

Veena_Vikram_2-1662372997393.png

 

 

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

 

Veena_Vikram_1-1662380625705.png

 

 

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

Veena_Vikram_2-1662380782464.pngVeena_Vikram_3-1662380841942.png

 

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 ✌

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

6 Replies

Avatar

Correct answer by
Community Advisor

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

Avatar

Employee

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

Avatar

Community Advisor

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



Arun Patidar

Avatar

Community Advisor

Yeah @Jörg_Hoh I have referred that post and it makes sense now. Thanks @arunpatidar and @Jörg_Hoh