How to get the path of the component instance under a page, that's invoking a servlet? | Community
Skip to main content
Level 2
May 24, 2018 2:53 PM UTC
Solved

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

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.
joerghoh
joerghohAccepted solution
Employee
May 27, 2018 2:49 PM UTC

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

15 replies

Ratna_Kumar
Level 10
Aug 15, 2018 12:29 PM UTC

Hey Scott,

This works very nicely. I have tested this code snippet and this works!! Below is the screenshot.

Thanks,

Ratna Kumar.

Translate
Level 4
Aug 15, 2018 4:21 AM UTC

Thanks Scott & Jorge..

This code sample is really helpful.

Translate
smacdonald2008
Level 10
Aug 15, 2018 2:57 AM UTC

Result of an AJAX CALL -

Translate
Level 2
Aug 26, 2020 9:42 PM UTC
can anyone share the project reference url instead the screenshot?
Translate
smacdonald2008
Level 10
Aug 15, 2018 2:53 AM UTC

Joerg is correct - although the Sling Servlet docs say both ways are supported - Apache Sling :: Servlets and Scripts - Reg by Resource type is better practice.

When you build a Maven 13 archetype, it builds a servlet that is OOTB bound to the page that is created. For example - in a package named com.aem.community.core.servlets you will find  SimpleServlet.

For example- see this (I added "sling.servlet.selectors=" + "groups") :

@Component(service=Servlet.class,

           property={

                   Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

                   "sling.servlet.methods=" + HttpConstants.METHOD_GET,

                   "sling.servlet.resourceTypes="+ "AEMMaven13/components/structure/page",

                   "sling.servlet.selectors=" + "groups"

           })

public class SimpleServlet extends SlingSafeMethodsServlet {

    private static final long serialVersionUid = 1L;

    @Override

    protected void doGet(final SlingHttpServletRequest req,

            final SlingHttpServletResponse resp) throws ServletException, IOException {

        final Resource resource = req.getResource();

        resp.setContentType("text/plain");

        resp.getWriter().write("COOL Title = " + resource.adaptTo(ValueMap.class).get("jcr:title"));

This is bound too this page:

Now you can call this Servlet by using this URL:

http://localhost:4502/content/AEMMaven13/en.groups.html

And you can use this same URL to invoke this SERVLET via an AJAX CALL (that is called when a button is clicked for example):

$(document).ready(function() {

   

    $('body').hide().fadeIn(5000);

          

$('#submit').click(function() {

    var failure = function(err) {

             alert("Unable to retrive data "+err);

   };

   

 

   var claimId = 0;

   

    //Use JQuery AJAX request to post data to a Sling Servlet

    $.ajax({

         type: 'GET',   

         url:'http://localhost:4502/content/AEMMaven13/en.groups.html',

         success: function(msg){

           var myMsg = msg;

 

 

            alert(myMsg);

 

         }

     });

  });

      

}); // end ready

Translate
joerghoh
Employee
Aug 14, 2018 7:32 PM UTC

see [1] for an example where the selector "tagwidget" is bound to all resource types; you might want to be more specific with the resource type.

[1] acs-aem-commons/TagWidgetConfigurationServlet.java at 763d2968169be1d86937bc1f4354cf2f8e201866 · Adobe-Consulting-Servic…

Translate
Level 4
Aug 13, 2018 3:41 PM UTC

Hi Jorge,

Do you have any sample code for above approach?

Thanks,
Rajeev

Translate
joerghoh
joerghohAccepted solution
Employee
May 27, 2018 2:49 PM UTC

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

Translate
arunpatidar
New Participant
May 25, 2018 7:26 PM UTC

There is a way to achieve this by making call to Sling servlet (which is registered via resourcesType="componentPath") using java and get component path using request.getResource().getPath()

But this required credential to make a succesful request in Author, I think you can do it without credential(Authentication) in the Publish instance.

I don't know if this is feasible or not from your end but I am able to do it in my local instance.

Below is JS file which making kind a ajax call from component

// Server-side JavaScript for the ajax call

use(function () {

    var curRes = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+resource.getPath();

    var getVar = new Packages.org.apache.commons.httpclient.methods.GetMethod(curRes);

    var client = new Packages.org.apache.commons.httpclient.HttpClient();

   // Authentication part

    var creds = new Packages.org.apache.commons.httpclient.UsernamePasswordCredentials("admin", "admin");

     client.getParams().setAuthenticationPreemptive(true);

    client.getState().setCredentials(org.apache.commons.httpclient.auth.AuthScope.ANY, creds);

   // Authentication part end

    client.executeMethod(getVar);

});

Thanks

Arun Patidar

Translate
Arun Patidar
smacdonald2008
Level 10
May 25, 2018 1:45 PM UTC

That will give you path to page - then you still need to figure out which component on the page sent the AJAX request.

Translate
Level 2
May 25, 2018 11:59 AM UTC

Hi,

You can register your servlet using resourceType and can get component path in your servlet using :

Translate