How to get currentpagepath in servlet | Community
Skip to main content
Level 2
May 19, 2024
Solved

How to get currentpagepath in servlet

  • May 19, 2024
  • 3 replies
  • 1379 views

How to  get current page path by using sling servlet and
display the page name in Htl.

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 sarav_prakash

Please accept answer and close the question if its clarified.

3 replies

Level 2
May 19, 2024

can anyone suggest

 

sarav_prakash
Community Advisor
Community Advisor
May 19, 2024

Your direction sounds wrong. If you are writing HTL component like PageTitle component that must render page name, you dont need servlets, instead use Sling models to render the page name server side. 

 

Idea is, the page originates at AEM serverside. While AEM prepares the page, you attempt to render most content at serverside itself using HTL. Then page is dispatched out of AEM and sent to browser. Now, sometimes you might need additional content rendered, AFTER initial page loads on browser. This is called Clientside rendering. For clientside purpose, you make second trip to AEM server using servlets, fetch the extra data and render. 

AEM recommendation/preference must be, try rendering serverside as much as possible

 

In your question, sounds you want to render the page name in a custom component using HTL. You dont require servlets. Correct way, your HTL looks like this,

 

<sly data-sly-use.page="com.adobe.cq.wcm.core.components.models.Page"> <h1>${page.title}</h1> </sly>

 Page is an ootb sling model that already has logic to return correct title.

 

Now if you still mandatory need servlet only, planning to make second AEM trip for page name, your servlet will look like this 

@8220494(service = { Servlet.class }) @SlingServletResourceTypes( resourceTypes="project/components/demo", methods=HttpConstants.METHOD_GET) @ServiceDescription("Demo Component ResourceType Servlet") public class DemoResourceTypeServlet extends SlingSafeMethodsServlet{ @Override public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { response.getWriter().write(request.getResource().getName()); } }

Here `resourceType` will be the type used in Page template. Once this servlet deployed, you ll call the servlet through your calling page. For example /content/demo/page.demo.json. This ll use the calling page as resource and return respective name.

Level 2
May 19, 2024

I am just want to get page title with the help of current pagepath in servlet.

 

sarav_prakash
Community Advisor
Community Advisor
May 19, 2024

Then your answer is already provided. Write a servlet like this

@8220494(service = { Servlet.class }) @SlingServletResourceTypes( resourceTypes="project/components/demo", methods=HttpConstants.METHOD_GET) @ServiceDescription("Demo Component ResourceType Servlet") public class DemoResourceTypeServlet extends SlingSafeMethodsServlet{ @Override public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) { response.getWriter().write(request.getResource().getName()); } }

This will return the page name of the page you call this servlet from. 

sravs
Community Advisor
Community Advisor
May 22, 2024

Hi @venkat_saiga ,

 

To display the current page name/title in HTL you can use the HTL global objects and get the property as required.

 

Please refer https://experienceleague.adobe.com/en/docs/experience-manager-htl/content/global-objects

 

 

EstebanBustamante
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
May 23, 2024

Hi,


A couple of things: if you want to display the page title in HTL, you should not use a servlet. Instead, you should use a Sling model. In the Sling model, you can inject the page from where the model is being invoked and then simply retrieve the title and return the value to HTL. It would look something like this:

package com.test.core.models; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import com.day.cq.wcm.api.Page; @Model(adaptables = SlingHttpServletRequest.class) public class PageTitleModel { @Self private SlingHttpServletRequest request; @SlingObject private Page currentPage; public String getPageTitle() { if (currentPage != null) { return currentPage.getTitle(); } else { return "No title available"; } } }

 And in HTL you will use like this:

<sly data-sly-use.model="com.test.core.models.PageTitleModel"></sly> <p>${model.pageTitle}<p>

Where pageTitle matches the getPageTitle() method in the sling method


Hope this helps.

Esteban Bustamante