Expand my Community achievements bar.

Adobe Summit 2025: AEM Session Recordings Are Live! Missed a session or want to revisit your favorites? Watch the latest recordings now.

request.getRequestDispatcher("/content/aem/en_us.html").include(request,response) is not working

Avatar

Level 1

I'm have a page with a template where all the components are configured in initial layout. Now I have created a new template with different resourceType and configured few components in initial layout and few components in structure as per my requirement. Since the page is dynamic and the data comes from a 3rd party service based on the ID from URL, I have to forward the request from the old page to new page which I have created and the ID will be appended to the new page so that dynamic data comes. The old page URL remains same but the layout changes based on the new page.

Now I'm facing issue when the components are configured in the initial layout and the components under structure are loading in the page. But when I manually open the new page all the components are loading in the page.

 

String newPath = "/content/aem/en_us/product.html";

String oldPath = "/content/aem/en_us/categories.html"

Resource res = request.getResourceResolver().getResource(newPath);

if(res!=null){

RequestDispatcher dispatcher = request.getRequestDispatcher(newPath);

dispatcher.include(request, response);

}

else{

RequestDispatcher dispatcher2 = request.getRequestDispatcher(oldPath);

dispatcher2.include(request, response);

}

 

Note: When I try to go to else condition the old page is loading with all the components, even though components were configured in initial layout of a template.

 

Any suggestions or any solutions please let me know.

 

3 Replies

Avatar

Community Advisor

Hi @Saiteja-PS,

So from what I understand, the issue likely comes down to how AEM handles structure and initial content when it renders a page.

See, structure components are actually part of the template, not the page’s own content node (jcr:content). When you're using RequestDispatcher.include(...), it’s only including the resource content - like /content/.../product.html- but it's not really kicking off the full page rendering process that would normally merge in structure components from the template.

That’s why your initial content is showing up (since it's part of the actual page content), but the structure components aren’t getting rendered unless you open the page directly in the browser.

What you could try
Sling forward() instead of include()

You might want to try replacing your include() with a forward() like this:

RequestDispatcher dispatcher = request.getRequestDispatcher(newPath);
dispatcher.forward(request, response);

This triggers a full Sling request processing cycle-just like when someone visits the page directly in their browser. It should also handle merging the structure properly.


Make sure you're pointing to the right resource

Also, just to be safe, check that newPath is pointing to the actual page (like /content/aem/en_us/product.html) and not to a sub-resource like /jcr:content. Sometimes people try something like:

Resource res = resolver.getResource(newPath + "/jcr:content");

Which might return null, even though the page path itself is valid. The key is: you want to forward to the page, not just a component or child resource.

Advanced option: SlingRequestProcessor

If neither of the above work, and you're still stuck, another (more advanced) option is using the SlingRequestProcessor:

slingRequestProcessor.processRequest(request, response, request.getResourceResolver());

But honestly, in most cases, using forward() should solve the issue nicely.


Give that a try and let me know if it works better with forward()

Hope that helps!


Santosh Sai

AEM BlogsLinkedIn


Avatar

Level 1

Hi @SantoshSai, thanks for the response. There is small correction. When I try to forward the request to new page components under structure are loading but not the components under initial layout of new template. But in else condition how ever we will be forwarding request to the same old page where all the components are under initial layout which is working fine by rendering all the components. 

When I try to use forward instead of include the main problem is I have to show the page name, title or any open graph parameters through my page. In this case I need to attain my page url and details but I need to just change the layout using new page with new template. So for that I have been using include() instead of forward().

 

Hope that gave some more clarity on the issue. Please let me know if there is any solution, that would be really helpful.

Avatar

Community Advisor

@Saiteja-PS 

If you want to keep the old page as the primary -  Maybe try this: instead of directly including the entire new page, you could break your layout into sub-components and include them individually. That way, you control what layout pieces get pulled in while still rendering them inside the original page request.

Alternatively, if possible, move some of those "structure" components from the new template’s structure section into the initial layout instead - just for the purpose of inclusion.


Santosh Sai

AEM BlogsLinkedIn