How to get the path of the component instance under a page, that's invoking a servlet? | Community
Skip to main content
New Participant
May 24, 2018
Solved

How to get the path of the component instance under a page, that's invoking a servlet?

  • May 24, 2018
  • 14 replies
  • 18720 views

Hi All,

Here's my requirement.

I have a component which is invoking the servlet via an Ajax call.

I would like to get the path of this component's instance under the page in the servlet.

If test is the page and comp1, comp2 are the instances of the same component dragged and dropped twice in the parsys called par,

I would like to get the path /content/test/jcr:content/par/comp1 in my servlet invoked from comp1, similarly /content/test/jcr:content/par/comp2 for comp2.

Alternatively, I am currently achieving this by using the resource HTL Global Object ${resource.path} in the JavaScript which gives the path of the current resource instance and passing it as a request parameter to the servlet.

But this is not admissible as I am trying to eliminate any query parameters being passed to the servlet due to caching constraints.

Is there a way to get path of the component instance under par which invokes the servlet at the back-end???

Please Help!!

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 joerghoh

Your requirement is quite easy to solve.

I assume that:

* the page is https://mycorp.com/content/mycorp/test.html

* and the servlet invoked is

** https://mycorp.com/bin/myservlet?comp=/content/mycorp/test/jcr:content/comp1 and

** https://mycorp.com/bin/myservlet?comp=/content/mycorp/test/jcr:content/comp2

and the servlet calls should be changed so the query string goes away.

Just change it in the way, that

* the page is https://mycorp.com/content/mycorp/test.html

* the servlet is called like this

** https://mycorp.com/content/mycorp/test/jcr:content/comp2.myservlet.json

** https://mycorp.com/content/mycorp/test/jcr:content/comp2.myservlet.json

And that's quite easy to achieve, because you have to bind the servlet as a selector to the resourcetype of "comp". The result is a perfectly cachable URL.

regards,

Jörg

14 replies

S_BhavyaAuthor
New Participant
May 25, 2018

The actual requirement is cache a servlet response, which is returned by a third party rest-api. The url for this rest-api is fetched from the dialog proeprties of the component 'comp1'.

So, I was hoping if I could get the path of the component instance under par from which the servlet invoked, I can directly fetch the dialog property using PropertyIterator without having to pass query params to the servlet. It would help me cache the response, provided I have an extension for the  servlet path. And when the author changes the URL in the dialog for that particular component and activates the page, the dispatcher might invalidate the cache and invoke the servlet again to fetch fresh response.

@edubey

Yes I totally agree with your point. This would be the next challenge. Any suggestions?

I have tried considering registering it with resourceTypes, selectors, extension and suffix. But I cannot bind the servlet to a single page resource as I would be using this servlet on multiple pages through various components serving the same purpose. Also, we cannot use wildcards to define a regex for the selector value while registering a servlet.

Alternatively I have also tried dispatcher level configurations such as ignoreUrlParams, Mod Rewrite Rules and resource mapping using etc/maps/http. It doesn't seem to work for my requirement.

Any suggestions?

I'm clearly going forward based on trial and error method.

Kindly correct me if my understanding is wrong.

edubey
New Participant
May 24, 2018

Ajax call in front end will be completely unware of component structure in AEM node.

You wil have to pass params.

One more, Even if you dont use params and  think of caching, Ideally your servlet response should not be cached else it will not execute again for another component.

Whats the use case?

smacdonald2008
New Participant
May 24, 2018

What problem are you trying to solve by getting this path. I am trying to understand what value this  is within a web site.