I have a scenario where from one page in AEM, I need to call another AEM page in the same application and I need to pass some hidden parameters. I choose to do it via POST and below are the steps which I followed:
From page "A", I did a form submission via POST to the sling servlet and passed some parameters.
2. In the servlet, using request dispatcher I redirected the same request and response to a different page in doPost method using the following code snippet:
When I run the code, I am able to call the servlet through form submission but its not working. In the developer tool I see a 200 call but the page is not loading at all and I see the below error in browser:
If I try the same code in doGet method it works fine. Also if I use response.sendRedirect("/content/company/en/apps/welcomepage.html") it works fine too. But the problem with this is it initiates it as a new request to the page and it looses all the parameters which I get from the form submission. Could someone please let me know like how can I redirect a request to a page in AEM via POST since I need to pass some hidden parameters whic should not be visible in the url ?
In AEM every page (.html) has a primary type jcr:primaryType = cq:Page and there are set of scripts that are mapped with this primary type which intercepts request and renders a page differently based on factors like selector, type of request method (by default only GET is handled) etc. You can find these mapping scripts for cq:Page under the folder /libs/cq/Page(in AEM 6.2). One of script under this folder is external.POST.jsp and this is the script which is responsible for handling a POST request with “.external” selector. If you look at the implementation of external.POST.jsp you’ll notice that implementation is very simple, it just creates a wrapper around existing POST request and overrides getMethod() method of SlingHttpServletRequestWrapper class so that it returns GET and forward control page URL (by removing selector) with this wrapped request and response object.
I tried this implementation and I was able to redirect my request to a new page and also I am able to capture the request parameters. But the problem I am facing now is once I redirect it to a new page the page keeps on loading again and again. I am thinking the "Sling Post servlet" or "Default Servlet" is getting called again and again. Did anyone face this issue ?
You can use the "Recent requests" view in the web console ( /system/console/requests) to actually check what the request is internally doing. Every include and every forward is listed there. Should give you insight what's going on.