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
Adobe 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

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
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
Level 4
Aug 15, 2018 4:21 AM UTC

Thanks Scott & Jorge..

This code sample is really helpful.

Translate
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