Unable to redirect to another page in AEM using request dispatcher in sling servlet via POST

Avatar

Avatar

rohitn62196663

Avatar

rohitn62196663

rohitn62196663

18-10-2017

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:

  1. 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:

            

              request.getRequestDispatcher("/content/company/en/apps/welcomepage.html").forward(request, response);

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:

Content modified /content/company/en/apps/welcomepage

Status
200
Message
OK
Location

/content/company/en/apps/welcomepage

Parent Location/content/company/en/apps
Path/content/company/en/apps/welcomepage
Refererhttp://localhost:7502/en/apps/welcome-pages/companypage.html
ChangeLog
<pre></pre>

Go Back

Modified Resource

Parent of Modified Resource

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 ?     

Replies

Avatar

Avatar

rohitn62196663

Avatar

rohitn62196663

rohitn62196663

20-10-2017

Thanks bsloki and Jorg Hoh for the suggestion. I also found a similar solution in the following link too:
http://suryakand-shinde.blogspot.in/2016/07/aem-form-submission-handling-post.html

http://suryakand-shinde.blogspot.in/2016/07/aem-form-submission-handling-post.html

As per the above link:

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 ?

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

996

Correct Reply

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

996

Correct Reply

1.0K
Jörg_Hoh
Employee

20-10-2017

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.

Jörg