Expand my Community achievements bar.

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

Avatar

Level 4

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 ?     

11 Replies

Avatar

Level 10

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

Avatar

Level 4

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 ?

Avatar

Level 10

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.

Avatar

Level 4

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.

Avatar

Employee Advisor

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

Avatar

Level 4

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);*/ }

Avatar

Employee Advisor

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)

Avatar

Level 4

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

Employee Advisor

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