Expand my Community achievements bar.

SOLVED

How to read Content(.html or .json) from the content tree in .jsp files

Avatar

Level 1

I am looking for a simple and straightforward way to read content (.html and .json) in the .jsp files. Let's say that I have a page path (/content/geometrixx-media/en/mycustompage.json) how can I read the content of this page in my .jsp files? Note that .jsp file is not located in another component. I have tried to use HttpClient and GetMethod, but it's giving me "Unauthorized" access error when I executeMethod. I know for a fact that I can read the values in javascript and jquery, by just doing a "getJson" of the page. Any Ideas as how to read .json values produced in Content tree back into any .jsp files? 

 

Thanks for the help.

1 Accepted Solution

Avatar

Correct answer by
Level 2

Hi Amin,

Though using request processor is possible to accomplish what you seem to be doing the cleaner way would be probably doing a sling include of your component from each page and using selector on that component for a specific layout.

Pseudocode like:

Loop over the child pages

    <sling:include path="childpage/jcr:content/yournode" addSelectors="customview"/>

End loop

 

this way the customview.jsp could have the logic related to rendering the component in the list as what you need. The logic of rendering a node/component is best to stay as close to the component than else where.

_Nishant

View solution in original post

10 Replies

Avatar

Correct answer by
Level 2

Hi Amin,

Though using request processor is possible to accomplish what you seem to be doing the cleaner way would be probably doing a sling include of your component from each page and using selector on that component for a specific layout.

Pseudocode like:

Loop over the child pages

    <sling:include path="childpage/jcr:content/yournode" addSelectors="customview"/>

End loop

 

this way the customview.jsp could have the logic related to rendering the component in the list as what you need. The logic of rendering a node/component is best to stay as close to the component than else where.

_Nishant

Avatar

Level 2

See if request processor can help you achieve this. 

Sample Snippet would be

HttpServletRequest request = requestResponseFactory.createRequest("GET", "/content/geometrixx-media/en/mycustompage.json"); ByteArrayOutputStream out = new ByteArrayOutputStream(); HttpServletResponse response = requestResponseFactory.createResponse(out); requestProcessor.processRequest(request, response, resourceResolver); html = new String(out.toByteArray(), "UTF-8");

where the requestResponseFactory is injected using annotations.

Avatar

Level 10

What is your exact use case?  When you are in same server why do you want to make a call to json or html in jsp. instead in jsp use the resource (/content/geometrixx-media/en/mycustompage) directly.

Avatar

Level 1

gnishant wrote...

Hi Amin,

Though using request processor is possible to accomplish what you seem to be doing the cleaner way would be probably doing a sling include of your component from each page and using selector on that component for a specific layout.

Pseudocode like:

Loop over the child pages

    <sling:include path="childpage/jcr:content/yournode" addSelectors="customview"/>

End loop

 

this way the customview.jsp could have the logic related to rendering the component in the list as what you need. The logic of rendering a node/component is best to stay as close to the component than else where.

_Nishant

 

This is perfect! Exactly what I was looking for. It's much cleaner and better way of doing it, that I was originally going to use. 

Thank you.

Avatar

Level 1

@gnishant it looks like your solution is exactly what I am looking for, but forgive if this sounds like a very basic question, but what you mean by "requestResponseFactory is injected using annotations"? I tried to copy the code you had into my .jsp file, and I get an exception. Am I supposed to do something else like import a package or something for this work. 

 

An error occurred at line: 27 in the generated java file

Only a type can be imported. sun.misc.RequestProcessor resolves to a package

Avatar

Level 2

Hi Amin

 

My Bad the injection part is done via annotation @Reference, but that will work in Servlet or OSGI Services. If you are coding directly in JSP try fetching the requestProcessor and requestResponseFactory as OSGI services. Sample code would be

SlingRequestProcessor requestProcessor = sling.getService(SlingRequestProcessor.class); RequestResponseFactory requestResponseFactory = sling.getService(RequestResponseFactory.class);

the sling object should be available by default in JSP.

Hope this helps

Nishant 

Avatar

Level 1

HI Nishant,

  I tried the RequestProcessor snippet that you had provided with no luck. It seems to be throwing a java.lang.StackOverflowError error. In one of the jsp pages i've this code:

SlingRequestProcessor requestProcessor = sling.getService(SlingRequestProcessor.class);
    RequestResponseFactory requestResponseFactory = sling.getService(RequestResponseFactory.class);
    HttpServletRequest req = requestResponseFactory.createRequest("GET", "/content/geometrixx.html");
    
    ByteArrayOutputStream outByte = new ByteArrayOutputStream();
     
    HttpServletResponse res = requestResponseFactory.createResponse(outByte);
    requestProcessor.processRequest(req, res, resourceResolver);
     
    String html = new String(outByte.toByteArray(), "UTF-8");
    out.println(html);

Please let me know if I'm missing something here.

Thanks,

BG

Avatar

Level 1

It seems to be working fine with /content/geometrixx.html. I had tried the same with a folder and that's when it threw that exception.

Thanks,

BG

Avatar

Employee

If you are getting a StackOverflowError, that suggests you've created an endless loop. Is this code is being executed from one of the components used on /content/geometrixx.html? You should be able to see what is happening by looking at the stack trace produced by the overflow error.

Avatar

Level 10

I am not sure what you are trying to so -- typically when you are getting values in AEM -- you are reading values from the JCR (a specific content tree node) or getting value back from an OSGi operation. Can you clarify please.