Highlighted

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

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

18-10-2017

Just wondering here - are you following an online doc for this use case - where did you learn this was best practice?

Highlighted

Avatar

Avatar

rohitn62196663

Avatar

rohitn62196663

rohitn62196663

18-10-2017

Its just the thought I got. The use case is to call a page on AEM by passing some hidden params to the page which are used for some business reasons. I would appreciate if you could let me know if there is any other way to do it ?

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

18-10-2017

I have never read an article on this. That is why I asked. 

I am searching to see what would be considered best practice for this use case.

Highlighted

Avatar

Avatar

rohitn62196663

Avatar

rohitn62196663

rohitn62196663

18-10-2017

Thank you !!

I am looking for a way to call a page in AEM via POST. I am able to do the same in a non AEM application.

Highlighted

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Answer

2.3K
smacdonald2008

18-10-2017

Here is some AEM Sample code that may help and shows use of this API:

aem-sample-we-retail/CartEntryServlet.java at master · Adobe-Marketing-Cloud/aem-sample-we-retail · ...

Highlighted

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K
Jörg_Hoh
Employee

18-10-2017

The problem is very simple: Your servlet is not called. You get back the output of the DefaultPostServlet, which just persists (or tries to persist) the posted parameters on the resource.

Make sure that you register your own servlet correctly. What you could do is to register it to a certain selector and then do the POST request to /contente/mysite/mypage.myselector.html; then your servlet should be called.

Jörg

Highlighted

Avatar

Avatar

bsloki

MVP

Total Posts

1.9K

Likes

172

Correct Answer

544

Avatar

bsloki

MVP

Total Posts

1.9K

Likes

172

Correct Answer

544
bsloki
MVP

18-10-2017

You can also refer this thread

Using forward(request, response) in Java Servlet

Highlighted

Avatar

Avatar

rohitn62196663

Avatar

rohitn62196663

rohitn62196663

18-10-2017

Thanks for the reply !! Below is the code snippet:

Servlet code:

@SlingServlet(paths = { "/bin/rap/welcomepage" }, methods = {"GET,""POST"})

public class RapWelcomePageServlet extends  SlingAllMethodsServlet {

/**

*

*/

private static final long serialVersionUID = 7184595514100008389L;

@Override

protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

throws ServletException, IOException {

Map<String,String> paramMap = request.getParameterMap();

request.setAttribute("WelcomePageParams", paramMap);

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

}

@Override

protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)

throws ServletException, IOException {

doGet(request,response);

}

}

UI code:

        var form = jQuery('<form></form>');

         form.attr("method", "post");

form.attr("action", "/bin/rap/welcomepage");

         jQuery(document.body).append(form);

         form.submit();

While submittion th form in the action attribute, I have given the servlet path. What else am I missing here for POST request ?
Could you please give me an example ?

@Override protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { //Map<String,String> paramMap = request.getParameterMap(); /*Enumeration paramNames = request.getParameterNames(); HttpSession session = request.getSession(false); while(paramNames.hasMoreElements()){ String element = (String) paramNames.nextElement(); String value = request.getParameter(element); //response.setHeader(element, value); session.setAttribute(element, value); }*/ GetRequest getRequest = new GetRequest(request); request.getRequestDispatcher("/content/pnc-com/en/apps/appointment-setting/schedule-appointment.html").forward(getRequest, response); //response.sendRedirect("/content/pnc-com/en/apps/appointment-setting/schedule-appointment.html"); } @Override protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException { doGet(request,response); /*Map<String,String> paramMap = request.getParameterMap(); request.setAttribute("WelcomePageParams", paramMap); //response.getWriter().write("success welcome page servlet"); request.getRequestDispatcher("/en/personal-banking.html").forward(request, response);*/ }

Highlighted

Avatar

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K

Avatar

Jörg_Hoh

Employee

Total Posts

3.0K

Likes

910

Correct Answer

1.0K
Jörg_Hoh
Employee

19-10-2017

Ok, looks like the problem is a bit different. Your servlet is called, and for me it seems that the forward is actually working. But not as expected.

I think that after you called

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

request.getMethod() still returns "POST". Have you tried to wrap the request and return in the getMethod() method a "GET" (which is likely what you want. [1] can help you to easily do this.

Jörg

ps: Just found that bsloki​ was already referring to the correct solution (another post in this forum). So the credit should go to him!

[1] SlingHttpServletRequestWrapper (Apache Sling 7 API)