request.getRequestDispatcher("/content/aem/en_us.html").include(request,response) is not working
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.