Create a Servlet to read a property from Multiple pages | Community
Skip to main content
Level 2
November 21, 2018

Create a Servlet to read a property from Multiple pages

  • November 21, 2018
  • 2 replies
  • 5752 views

Hi All,

I have a requirement to create a Sling Servlet that will get the root path of my website. It should loop through the children pages and read a particular property and expose the page url and the property as JSON. I want this to be displayed in the frontend by creating a HTL component that will get the data from this servlet. Could you please suggest the best approach that can be followed to accomplish this?

@​

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

smacdonald2008
Level 10
November 21, 2018

This is quite possible with HTL and Sling Models, You would need to create a BEAN on the Java side to store all props. THen have logic in Java to read all of the child pages and read the props that you want. I would look at using JCR SQL2 to perform the query:

String sqlStatement = "select * from [cq:Page] where isdescendantnode('/content/we-retail') "

Then iterate through the result set and get the props you want. in the Java - use GSON (or another JSON Java lib) to construct the JSON too.

smacdonald2008
Level 10
November 21, 2018

The value /content/we-retail can be passed to sling model via a path finder in component dialog. 

smacdonald2008
Level 10
November 21, 2018

Also be aware that looping through all the pages in a result set will be bad performance - esp if you want to display in a component. If you need to get that data for some reason - it makes more sense to run in a workflow - where it will not impact performace of your site.

lokeshb93001514
November 21, 2018

Why only a servlet? Best way is to write a sling model and call the service from that where the service will return the object with the necessary properties.

Level 2
November 22, 2018

I'm working on an application as part of an assignment and the Servlet was already provided. My job is to read the JSON received as response from the Servlet and display it on the page using HTL.

arunpatidar
Community Advisor
Community Advisor
November 22, 2018

Why don't you use client side approach,  make a Ajax request to servlet and get the son response and parse the json response and update DOM using jQuery.

Arun Patidar