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 java.net.HttpUrlConnection 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  org.apache.sling.engine 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.

61 comments

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 org.apache.sling.engine it returns <dependency> <groupId>com.adobe.granite.sling</groupId> <artifactId>org.apache.sling.engine</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 com.adobe.granite.sling:org.apache.sling.engine:jar:2.2.8-B006 in adobe (http://repo.adobe.com/nexus/content/groups/public/) 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>org.apache.sling</groupId> <artifactId>org.apache.sling.engine</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.

-- | February 04, 2018 at 06:12 AM | Reply

Comment removed by author.

-- | February 04, 2018 at 05:56 PM | Reply

Comment removed by author.

-- | February 06, 2018 at 02:23 PM | Reply

Comment removed by author.

-- | February 08, 2018 at 02:21 PM | Reply

Comment removed by author.

-- | February 09, 2018 at 01:06 PM | Reply

Comment removed by author.

-- | February 11, 2018 at 04:44 AM | Reply

Comment removed by author.

-- | February 12, 2018 at 07:47 PM | Reply

Comment removed by author.

-- | February 22, 2018 at 11:52 AM | Reply

Comment removed by author.

-- | February 22, 2018 at 01:06 PM | Reply

Comment removed by author.

-- | February 23, 2018 at 12:16 AM | Reply

Comment removed by author.

-- | February 24, 2018 at 01:52 PM | Reply

Comment removed by author.

-- | February 27, 2018 at 12:29 PM | Reply

Comment removed by author.

-- | March 03, 2018 at 08:40 AM | Reply

Comment removed by author.

-- | March 05, 2018 at 07:44 PM | Reply

Comment removed by author.

-- | March 08, 2018 at 12:23 PM | Reply

Comment removed by author.

-- | March 09, 2018 at 07:41 PM | Reply

Comment removed by author.

-- | March 10, 2018 at 03:46 PM | Reply

Comment removed by author.

-- | March 11, 2018 at 02:48 AM | Reply

Comment removed by author.

-- | March 13, 2018 at 12:36 AM | Reply

Comment removed by author.

-- | March 15, 2018 at 04:52 PM | Reply

Comment removed by author.

-- | March 15, 2018 at 08:28 PM | Reply

Comment removed by author.

-- | March 16, 2018 at 03:12 AM | Reply

Comment removed by author.

-- | March 17, 2018 at 07:53 PM | Reply

Comment removed by author.

-- | March 18, 2018 at 03:44 AM | Reply

Comment removed by author.

-- | March 20, 2018 at 04:45 AM | Reply

Comment removed by author.

-- | March 28, 2018 at 02:47 AM | Reply

Comment removed by author.

tnrtpi | March 30, 2018 at 07:33 AM | Reply

<a href=http://canadian-pharmacie.com>viagra online canadian pharmacy</a> viagra online melbourne australia http://canadian-pharmacie.com

Sam | April 05, 2018 at 06:01 PM | Reply

I tried this code with AEM 6.2 SP1 CFP10 and I'm getting a nullpointer Caused by: java.lang.NullPointerException at org.apache.sling.i18n.DefaultLocaleResolver.resolveLocale(DefaultLocaleResolver.java:53) I even tried it on with an OOTB text component. As soon as my component generate HTML it throws this null pointer.

Sam | April 09, 2018 at 08:17 PM | Reply

Proved that Adobe broke it. It is fixed in AEM 6.2 SP1 CFP13

bscdbj | April 13, 2018 at 05:31 PM | Reply

<a href=http://canadian-pharmacie.com>buy viagra in canada</a> buy generic viagra online mastercard http://canadian-pharmacie.com

aisgno | April 13, 2018 at 09:15 PM | Reply

<a href=http://cialisy.com>cheap generic cialis</a> consiglio acquisto cialis online http://cialisy.com

lwbddo | April 21, 2018 at 06:11 AM | Reply

<a href=http://canadian-pharmacye.com>cheap viagra online</a> generico viagra chile http://canadian-pharmacye.com

imigkp | April 25, 2018 at 12:01 PM | Reply

<a href=http://canadian-pharmacye.com>buy viagra no prescription</a> viagra einzeln online bestellen http://canadian-pharmacye.com

fvwoct | April 25, 2018 at 07:17 PM | Reply

<a href=http://canadan-pharmacy.com>viagra sample</a> comprar generico viagra http://canadan-pharmacy.com

bdxmpv | April 27, 2018 at 02:29 PM | Reply

<a href=http://canadian-pharmasy.com>acheter cialis</a> real cialis online uk http://canadian-pharmasy.com

Robertmok | April 29, 2018 at 09:47 AM | Reply

viagra without subscription <a href=http://viagrawithoutdoctorprescr.today/#4065>viagra without prescription</a> viagra without erectile dysfunction <a href=http://viagrawithoutdoctorprescr.today/#4667>viagra without a doctor prescription</a>

bmztcs | April 30, 2018 at 04:18 PM | Reply

<a href=http://canadian-pharmacie.com>online pharmacy viagra</a> is online viagra real http://canadian-pharmacie.com

Robertmok | May 01, 2018 at 01:26 AM | Reply

viagra without a doctor prescription from canada <a href=http://viagrawithoutdoctorprescr.today/#8651>viagra without prescription</a> viagra without a doctor prescription from canada <a href=http://viagrawithoutdoctorprescr.today/#1332>viagra on line no prec</a>

nmxqtd | May 01, 2018 at 12:51 PM | Reply

<a href=http://canadian-pharman.com>cheap viagra pills</a> buy cheap viagra usa where to get viagra online reddit http://canadian-pharman.com

Williamlor | May 03, 2018 at 09:49 PM | Reply

cialis online <a href=http://cialishg.us/#6105>cialis cost</a> cialis online

ndsqfj | May 04, 2018 at 03:52 AM | Reply

<a href=http://speedy-papers.com>buy essay fast</a> check essay online <a href=http://speedy-papers.com>ib extended essay topics</a> can someone write my essay http://speedy-papers.com

Robertmok | May 05, 2018 at 02:23 AM | Reply

viagra without a doctor prescription <a href=http://viagrawithoutdoctorprescr.today/#2769>viagra without a doctor prescription</a> viagra without insurance <a href=http://viagrawithoutdoctorprescr.today/#458>viagra without prescription</a>

Williamlor | May 05, 2018 at 02:57 PM | Reply

cialis <a href=http://cialishg.us/#3341>cialis on line no pres</a> cialis online

nazfkk | May 10, 2018 at 09:17 AM | Reply

<a href=http://canadian-pharmasy.com>buy cialis usa</a> cialis cost usa viagra cialis levitra online pharmacy http://canadian-pharmasy.com

mgyggh | May 21, 2018 at 08:59 AM | Reply

<a href=http://canadian-viagracy.com>buy female viagra</a> viagra online pay with paypal australia http://canadian-viagracy.com

erjsfx | May 22, 2018 at 05:55 PM | Reply

can viagra be purchased online <a href=http://viagrasy.com>buy viagra overnight delivery</a> viagra online apotheke ohne rezept http://viagrasy.com

cganyv | May 23, 2018 at 08:13 PM | Reply

<a href=http://canadian-pharmasy.com>cialis super active</a> cialis online pharmacy usa comprar cialis online http://canadian-pharmasy.com

mqtlum | May 29, 2018 at 08:48 AM | Reply

<a href=http://canadian-pharmacie.com>buy real viagra</a> where to buy generic viagra forum http://canadian-pharmacie.com

ddtslb | June 03, 2018 at 12:26 AM | Reply

<a href=http://canadian-pharman.com>female viagra pill</a> buy viagra online usa buy generic viagra in uk http://canadian-pharman.com

nbosfi | June 12, 2018 at 06:03 AM | Reply

http://cialisn.com - buy sale cialis cheap cialis http://cialisn.com

nnrkde | June 12, 2018 at 06:20 PM | Reply

<a href=http://canada-pharmaci.com>professional cialis 20mg</a> buy cialis online with prescription http://canada-pharmaci.com

dzbmtn | June 15, 2018 at 01:05 AM | Reply

<a href=http://canadian-pharmasy.com>cheap cialis online</a> buy cialis in usa cialis sale online canada http://canadian-pharmasy.com

pntxfs | June 18, 2018 at 07:11 AM | Reply

order viagra online overnight delivery <a href=http://canadian-pharmacyc.com>generic viagra</a> generic viagra means http://canadian-pharmacyc.com

Leave a Comment