Servlet in AEM to print html content on hitting with .html extension | Community
Skip to main content
Himanshu_Singhal
Community Advisor
Community Advisor
January 15, 2016
Solved

Servlet in AEM to print html content on hitting with .html extension

  • January 15, 2016
  • 2 replies
  • 4153 views

Hi,

I've been looking a way to customize Asset Finder in Touch UI based to add custom tabs for references we've created for our projects. All I could find out from out of box functionality is if we hit this URL given below from our localhost:

URL : http://localhost:4502/bin/wcm/contentfinder/asset/view.html?itemResourceType=cq/gui/components/authoring/assetfinder/asset

It displays html content. And,if hit with .json extension, it displays json.

So, how can we create our custom servlet which display html content on hitting with .html extension and json on hitting with .json extension.

Like, on hitting above URL with .html extension, it displays image with some text properties. And, on hitting with .json, it displays properties key-value pairs.

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 kautuk_sahni

As mentioned by Jitendra, we can Script resolution to achieve this.

Reference link:- https://www.youtube.com/watch?v=ySw7vE1hdT8

How Sling resolution is done in AEM:- http://www.aemcq5tutorials.com/tutorials/sling-resource-resolution-aem/

//

If you’ve worked on AEM much, you should have at least been exposed to this diagram:

apache-sling

There’s a lot going on here, but I’m just going to focus on query parameters, selectors, and suffixes.

Link:- http://aempodcast.com/2013/apache-sling/sling-request-suffixes-explained/#.VpxqJvl97RY

 

Option 2:- Using Sling Servlet

Link:- http://stackoverflow.com/questions/29102285/how-to-invoke-sling-servlet-that-uses-resourcetype-instead-of-paths-in-slin

//

Instead of making ajax call to the path in the servlet, you make an ajax call to the component. In case you want the servlet to work with resourceType the servlet should have an additional configuration for extensions property (sling.servlet.extensions).This configuration let's you run a servlet in context of a resource(of a particular resourceType) instead of a global one.

Let me explain with an example. Consider a page content/home.html with a foo component (resourceType="/apps/someproject/components/foo) at path /par/foo . Normally on a page the component will be requested with .html selector and the resource will be rendered by the default script (foo.jsp). Let's add a servlet with the following annotation

@SlingServlet( name="Weatherservlet", extensions = "pdf", resourceType="someproject/components/foo", methods="GET", metatype=true) which will give pdf representation of the resource.

A GET request to /content/home/jcr:content/par/foo.pdf will be handled by the servlet instead offoo.jsp.

request.getResource() inside the servlet's doGet will return the component resource.

path configuration will override the resourceType configuration.

 

I hope this would help you. 

Thanks and Regards

Kautuk Sahni

2 replies

Jitendra_S_Toma
Level 10
January 15, 2016

Hi Himanshu,

Here is the article which talks about SlingServlet.

http://blogs.adobe.com/aaa/2012/09/cq-tips-and-tricks-1-how-to-define-a-slingservlet-cq5-5-5-6.html

There are multiple ways to achieve it. One option is, build a servlet which have html & json both selectors and based on selector from the request URL, you could build appropriate response.

The second option could be sling script resolutions. Here is the doc for more details. Let me know if you need actual details about it.

https://sling.apache.org/documentation/the-sling-engine/url-to-script-resolution.html

Jitendra

kautuk_sahni
Community Manager
kautuk_sahniCommunity ManagerAccepted solution
Community Manager
January 18, 2016

As mentioned by Jitendra, we can Script resolution to achieve this.

Reference link:- https://www.youtube.com/watch?v=ySw7vE1hdT8

How Sling resolution is done in AEM:- http://www.aemcq5tutorials.com/tutorials/sling-resource-resolution-aem/

//

If you’ve worked on AEM much, you should have at least been exposed to this diagram:

apache-sling

There’s a lot going on here, but I’m just going to focus on query parameters, selectors, and suffixes.

Link:- http://aempodcast.com/2013/apache-sling/sling-request-suffixes-explained/#.VpxqJvl97RY

 

Option 2:- Using Sling Servlet

Link:- http://stackoverflow.com/questions/29102285/how-to-invoke-sling-servlet-that-uses-resourcetype-instead-of-paths-in-slin

//

Instead of making ajax call to the path in the servlet, you make an ajax call to the component. In case you want the servlet to work with resourceType the servlet should have an additional configuration for extensions property (sling.servlet.extensions).This configuration let's you run a servlet in context of a resource(of a particular resourceType) instead of a global one.

Let me explain with an example. Consider a page content/home.html with a foo component (resourceType="/apps/someproject/components/foo) at path /par/foo . Normally on a page the component will be requested with .html selector and the resource will be rendered by the default script (foo.jsp). Let's add a servlet with the following annotation

@SlingServlet( name="Weatherservlet", extensions = "pdf", resourceType="someproject/components/foo", methods="GET", metatype=true) which will give pdf representation of the resource.

A GET request to /content/home/jcr:content/par/foo.pdf will be handled by the servlet instead offoo.jsp.

request.getResource() inside the servlet's doGet will return the component resource.

path configuration will override the resourceType configuration.

 

I hope this would help you. 

Thanks and Regards

Kautuk Sahni

Kautuk Sahni