Get the rendered HTML for an AEM resource, component or page

Adobe Experience Manager | AEM/CQ | Apache Sling

Get the rendered HTML for an AEM resource, component or page

It's easy to retrieve a rendered component by making a GET request. Apache Sling's SlingMainServlet and DefaultGetServlet will process GET requests, take the path's extension into account, and return the rendered resource. The most obvious example of triggering this process is simply typing the component's path into the browser's address bar or making an AJAX call. You can retrieve the HTML markup (or JSON, XML, txt, PDF, etc...) for a component as well as a page if you provide the correct path. After all, they're both just Sling resources.

Trying to get the rendered HTML of a component on the server side is still easy, but requires a little more work. Without knowing the inner workings of Sling, you might use Java's class to construct and make an HTTP request from within your application to your application.

The better way is to use the SlingRequestProcessor service as the entry point into Sling's process. The processRequest method of the SlingRequestProcessor takes an HttpServletRequest, HttpServletResponse, and Resource Resolver as parameters. The only trick is that you need to provide a request that enables you to set the request path and a response that enables you to get the OutputStream as a String.

AEM provides the RequestResponseFactory where you can easily get such a request and response.

You'll need to provide your own mock request and response in Apache Sling. Fortunately, it already exists. You need the HttpRequest, HttpResponse, and TestServletOutputStream classes.

You will need to include the dependency in your pom file. If your bundle resolves but your component remains unsatisfied, make sure sure you've included the package in the Import-Package section of your maven-bundle-plugin.


Nate Yolles | December 10, 2015 at 08:21 AM | Reply

Using the SlingRequestProcessor you can get any rendering that your Sling instance provides. For example, you can get the component's JSON rendering if you change the requestPath to something like "/content/myapp/us/en/index.-1.json".

Homer | December 21, 2015 at 12:35 PM | Reply

I am using AEM 5.6.1 and when i use dep finder in system console for it returns <dependency> <groupId></groupId> <artifactId></artifactId> <version>2.2.8-B006</version> <scope>provided</scope> </dependency> But when i tried to add to my pom I am getting build error Could not find artifact in adobe ( Have you encountered this as well? Any workaround you can suggest. Thanks!

radicaledward101 | May 23, 2016 at 04:34 PM | Reply

I was able to get this to work using the following: <dependency> <groupId></groupId> <artifactId></artifactId> <version>2.4.6</version> <scope>provided</scope> </dependency>. Your version number may be different depending on your version of AEM, but you should chop off the -B### portion. Your build should not need to know the exact build number since the dependency is provided anyway.

Gdubz | June 28, 2016 at 04:11 PM | Reply

When using the AEM implementation, my response is polluted with a bunch of head/meta stuff that a normal page for my project would get. When visiting the resource directly in the browser (with .html at the end), I'm not seeing any of that, just clean <html><head></head><body> /* all the parsys content here */ </body></html>... Am I doing something wrong? I'm guessing the Sling candidate servlet when viewing in browser is different than the SlingRequestProcessor we're using in the code above?

Gdubz | June 28, 2016 at 04:15 PM | Reply

Please disregard this comment, I'm stupid and should probably have doublechecked WHAT I was attempting to resolve before posting. All good now!

APair | July 27, 2016 at 05:37 PM | Reply

Hi, thanks for this sample code :-) I've tried both AemResource and SlingResource methods above, to retrieve rendered HTML of one component, with partial success...: - on author instance, it works perfectly fine! - on publish instance however, part of HTML markup is missing :-(, using both above methods. Mainly, some <a href>s that have some sightly markup in them... To make it even more confusing, doing request to the same path with browser - I see correct and complete output on publish instance... only when calling through above requests methods in java and outputting in servlet, it is missing :-( I continue looking at this problem, but I must say I am out of new ideas on how to debug it... content is there published to publish instance as it renders fine on pages... just when I need it in servlet it is gone... Thanks in advance for dropping me few ideas...

Kartik | May 19, 2017 at 02:22 PM | Reply

Hey, I am also facing the same issue, in publish server, some markups are missing? Did you find anything to fix this issue?

APair | July 27, 2016 at 08:30 PM | Reply

Doh... I've found what is it - got other ideas, that maybe problem is not with the code, not with component, not with sightly/template. And I've found LinkChecker to be removing those links, and only in that specific scenario, DOH, murphy's laws.

AnthonyElord | April 29, 2019 at 03:31 PM | Reply

real penis enlargement pills <a href=></a> how to pass a drug test with household products

CharlesBom | May 12, 2019 at 03:01 AM | Reply

online prescription safety glasses <a href= ></a> over the counter toothache medicine

MichaelSkisa | May 19, 2019 at 06:51 PM | Reply

functioning magazines argentina computational <a href=>reproduction </a> prerequisite magnitude showtimes newfoundland disappointed

Isaacloala | May 21, 2019 at 11:39 PM | Reply

notebooks attraction mysterious conflicts

ArthurLom | May 24, 2019 at 02:33 AM | Reply

cost of health care <a href= ></a> prescription ray ban wayfarer glasses

Samuel98456 | May 31, 2019 at 09:51 PM | Reply

I need help fixing the <b>office error code message 30015-6with Word.</b> I followed the steps the link <b><a href=>error code 30180 4 outlook 2016 launch</a></b> I have followed the steps and fixed the error, but I need help with fixint the error with the templates now. Can anybody suggest the steps to fix the templates?

Thomasdaymn | June 03, 2019 at 04:21 AM | Reply

loveland community health center <a href= ></a> healthcare jobs in houston texas

Lucashar | June 15, 2019 at 07:47 PM | Reply

walmart prescription 4 dollar list <a href= ></a> best medicinal herbs to grow

Lucashar | June 27, 2019 at 07:56 PM | Reply

medco health prescription refill <a href= ></a> south river family medicine

canadianpharmacyndahip | August 17, 2019 at 10:48 AM | Reply

viagra canadian pharmacy generic <a href=>cheap viagra online canadian pharmacy</a> buy generic viagra online canada

canadianpharmacyndahip | August 17, 2019 at 04:08 PM | Reply

best canadian pharmacy <a href=>generic viagra online canadian pharmacy</a> canada generic viagra

Stevnami | September 05, 2019 at 11:56 AM | Reply

Difference Cialis Et Viagra Farmaci Kamagra Co Uk <a href=>buy viagra</a> Amoxicillin And Ear Infections Pfizer Website Viagra Sales

LesUsessy | November 13, 2019 at 05:09 AM | Reply

No Prescriptions Online Pharmacy Cialis Cialis And Levitra <a href=>buy cialis online</a> Ed Pills No Perscription Propecia Wirksamkeit Can Amoxicillin Go Bad

rahul | January 22, 2020 at 11:36 AM | Reply

can we keep extension as xml and render the overridden data in extension based servlet

ocggghok | March 09, 2020 at 08:11 PM | Reply

viagra online generic viagra <a href="">cheap viagra </a> buy viagra viagra 100mg viagra generic viagra generic <a href=>generic viagra </a> viagra online viagra online

rzfehhok | March 19, 2020 at 09:29 AM | Reply

generic viagra cheap viagra <a href=" #">generic viagra </a> buy viagra generic viagra buy viagra cheap viagra <a href=>viagra buy </a> cheap viagra buy viagra

wwmpthok | March 25, 2020 at 12:42 AM | Reply

when does viagra patent expire girl viagra prank <a href=" #">levitra vs viagra </a> reliable place to order cheap viagra online viagra is doomed the picture of the girl on the picture active ingredient in viagra buy viagra superfoods aruba <a href=>generic viagra available </a> purchasing viagra on the internet affordable viagra without a perception

HellRiz | April 22, 2020 at 09:18 PM | Reply

New animal porn with dogs <a href=>best child porn</a> dog fuck 11 years old little girl <a href="">child porn</a> <a href="">child porn</a> <a href="">child porn</a> <a href="">child porn</a> <a href="">child porn</a> <a href="">child porn</a> <a href="">child porn</a> <a href="">child porn</a> <a href="">child porn</a> <a href="">child porn</a> <a href="">child porn</a>

Leave a Comment