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
Solved

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

  • May 24, 2018
  • 14 replies
  • 18723 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

smacdonald2008
Level 10
August 15, 2018

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

smacdonald2008
Level 10
August 15, 2018

Result of an AJAX CALL -

Level 2
August 26, 2020
can anyone share the project reference url instead the screenshot?
Level 4
August 15, 2018

Thanks Scott & Jorge..

This code sample is really helpful.

Ratna_Kumar
Level 10
August 15, 2018

Hey Scott,

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

Thanks,

Ratna Kumar.