Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to get the current page path in a sling servlet that is called in the component dialog?

Avatar

Level 2

Hi,

I have a component that can be dragged and dropped onto a parsys in a page. The component dialog has a dropdown that gets the options from a servlet. 

In the servlet, I want to access the currentPage  path.   i.e the  page where the component was added.

I have tried to access it in the servlet using request.getRequestURI() and request.getResource().getPath() etc. But it points to the servlet path.

Can anyone let me know how I can the current page path in the servlet?

Thanks in Advance.

Regards,

Chaitanya Pai

1 Accepted Solution

Avatar

Correct answer by
Level 2

hi Chaitanya,

check the referer http header, it should contain what you are looking for.

 

An alve would be to read the current path in the dialog's javascript and add it as a parameter to the servlet.

 

cheers,

Ben

View solution in original post

3 Replies

Avatar

Correct answer by
Level 2

hi Chaitanya,

check the referer http header, it should contain what you are looking for.

 

An alve would be to read the current path in the dialog's javascript and add it as a parameter to the servlet.

 

cheers,

Ben

Avatar

Level 1

If we use 'referer' we may end-up with Critical sonar issue.

Instead we shall use 'RequestParametMap' and fetch 'item' key value, this is retuning exact page path.

Avatar

Former Community Member

Here's some examples for couple options:

  • OPTION 1 - Hidden Parameter with Sightly and Servlets

In your HTML form, add a hidden param:

        <input type="hidden" name="resourcePath" id="resourcePath" value="${currentPage.getPath}">

In your Servlet..

        if (slingHttpServletRequest.getParameter("resourcePath")!=null) { resourcePath = slingHttpServletRequest.getParameter("resourcePath");         }

Gives the path as /content/site1/en/abc.html

 

 

  • OPTION 2 - Just Servlets using HttpRequest Header
String resourcePath=""; // path of the page from where the request came from Enumeration values = slingHttpServletRequest.getHeaders("Referer"); if (values != null) { while (values.hasMoreElements()) { String url= (String) values.nextElement(); log.info("URL=" + url); } }       

Gives the path as http://localhost:4502/content/site1/en/abc.html