Expand my Community achievements bar.

SOLVED

How to get currentpagepath in servlet

Avatar

Level 2

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

1 Accepted Solution

Avatar

Correct answer by
Level 6

Please accept answer and close the question if its clarified.

View solution in original post

7 Replies

Avatar

Level 6

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 

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

Avatar

Level 2

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

 

Avatar

Level 6

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

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

Avatar

Correct answer by
Level 6

Please accept answer and close the question if its clarified.

Avatar

Community Advisor

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

 

 

Avatar

Community Advisor

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