Get Page Content (HTML) in a Sling Servlet | Community
Skip to main content
Level 3
October 16, 2015
Solved

Get Page Content (HTML) in a Sling Servlet

  • October 16, 2015
  • 3 replies
  • 2915 views

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

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

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.

3 replies

jocampAuthor
Level 3
October 16, 2015

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.

vikramca06
vikramca06Accepted solution
Level 4
October 16, 2015

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.

Level 2
October 16, 2015

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