Expand my Community achievements bar.

SOLVED

Get Page Content (HTML) in a Sling Servlet

Avatar

Level 4

What would be the most efficient way to retrieve a page's content/parsys rendered as html for a sling servlet? Supposing the user calls this servlet with a page path and the servlet just returns the html generated from the parsys on that page. Similar to how the <cq:include> tag works on the jsp side.

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi,

you can make an httpclient call with getmethod by setting the URL like "http://host:port/pagepath/jcr:content/parsys.html". So that it will return the html content of the parsys. Make sure you add the content type to the getmethod header before executing.

the response will be in html. So you can do what ever you want.

Thanks

Vikram.

View solution in original post

3 Replies

Avatar

Level 4

I found a similar question elsewhere and the answer said this is how cq:include is working -

request.getRequestDispatcher("page/path.html").include(request, response);

It's close to what i was looking for but really i just wanted to store the html into a variable and manipulate it instead of directly injecting it into the response stream.

Avatar

Correct answer by
Level 4

Hi,

you can make an httpclient call with getmethod by setting the URL like "http://host:port/pagepath/jcr:content/parsys.html". So that it will return the html content of the parsys. Make sure you add the content type to the getmethod header before executing.

the response will be in html. So you can do what ever you want.

Thanks

Vikram.

Avatar

Level 2

Try out SlingRequestProcessor class:

@Reference SlingRequestProcessor slingRequestProcessor; slingRequestProcessor.processRequest(HttpServletRequest request, HttpServletResponse response, ResourceResolver resourceResolver)

You need to create mock request and response objects. You might want to have a look at RequestResponseFactory