Expand my Community achievements bar.

SOLVED

JSP File - Display an AEM page

Avatar

Level 4

Hi community !

I have a JSP file and an AEM page, how do i make that JSP file to display my AEM page?

I can't find anything related to, it's that even possible ?

Edited: I'm also thinking at an internal redirect. This should also work for my purpose.


Thanks

1 Accepted Solution

Avatar

Correct answer by
Level 4

So, I've used this in my 404.jsp and I achieved what i wanted

<%@include file="/libs/foundation/global.jsp"%> 
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %><%
%><sling:defineObjects /><%
response.setStatus(404);
request.getRequestDispatcher("/content/demo/somepage.html").forward(request,response); %>

View solution in original post

10 Replies

Avatar

Community Advisor

Hi,

What are you using for display AEM page JSP or HTL?

JSP - you can try below to include JSP in AEM Page

  1. <%@ include file="resourceName" %> 
  2. <jsp:include page="..." />

HTL - data-sly-include is to use include other html/jsp resources.

Thanks

Arun



Arun Patidar

Avatar

Level 4

HI !

I have an JSP file and within this JSP file i want to display I page made in AEM which is found under /content/name-of-the-project/page-name

I have no clue how to bind them. I also thought about a redirect but my issue with is with the root path. How do i get the relative path ?

for now i'm using something like this   

     String redirectURL = "http://localhost:4502/content/demo/somepage.html";

but i want something like

     String redirectURL = "/demo/somepage.html";

Avatar

Administrator

I did not get your ask clearly. Please help us elaborate this.

-Kautuk



Kautuk Sahni

Avatar

Community Advisor

Hi

Can you share some example, where is your JSP and where is your AEM page and why can't you use path from root?

or if you setup vanity URL to avoid /content/ is this feasible or not?

Please help us to replicate or implement.

Thanks

Arun



Arun Patidar

Avatar

Level 4

So

I've overlayed the the script under the libs/sling/servlet/errorhandler/ so i can have my own custom 404 error script. And for that I want to use a page which I made on AEM.

So my JSP script is under /apps/sling/servlet/errorhandler/404.jsp and my page is under /content/demo/404

How can i bind them ?

That's what I'm willing to achieve

Avatar

Correct answer by
Level 4

So, I've used this in my 404.jsp and I achieved what i wanted

<%@include file="/libs/foundation/global.jsp"%> 
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %><%
%><sling:defineObjects /><%
response.setStatus(404);
request.getRequestDispatcher("/content/demo/somepage.html").forward(request,response); %>

Avatar

Community Advisor

Hi,

You can use HTL also for creating custom error pages functionality

for that you need to create 404.html and ResponseStatus.java inside /apps/sling/servlet/errorhandler

(You may create ResponseStatus.java outside repo and deployed as part of bundle)

Sample codes below :

404.HTML

<sly data-sly-use.responseStatus="apps.sling.servlet.errorhandler.ResponseStatus" />

<sly data-sly-resource="${'/content/AEM63App/en/page-not-found.html'}"></sly>

ResponseStatus.java

package apps.sling.servlet.errorhandler;

import com.adobe.cq.sightly.WCMUsePojo;

public class ResponseStatus extends WCMUsePojo {   

    @Override

    public void activate() throws Exception {

        getResponse().setStatus(404);

        getResponse().setContentType("text/html");

    }

}

Screen Shot 2018-06-13 at 3.59.38 PM.png

Thanks

Arun



Arun Patidar

Avatar

Employee Advisor

And please please, pay attention to the HTTP response statuscode! arunp99088702​'s example demonstrates how to do it right.

Jörg

Avatar

Level 4

That's really helpful, thanks!!

But i can't really make it work with error 500.

I get this:

Error while processing /content/ui-centre/500.html

Status

500

Message

org.apache.sling.api.SlingException: Exception during response processing.

Location/content/ui-centre/500.html
Parent Location/content/demo
Path

/content/ui-centre/500.html

Refererhttp://localhost:4502/content/demo/page.html
ChangeLog

<pre></pre>

Go Back

Modified Resource

Parent of Modified Resource

Avatar

Employee Advisor

That's the error as displayed by the Sling Post Servlet. If you want to have proper error handling for them as well, at the moment it's not possible to handle both POST and GET the same way. See [1] for a way howto implement it.

Jörg

[1] [SLING-7552] SlingPostServlet error handling still insufficient - ASF JIRA