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