Page not redirecting on form submit | Community
Skip to main content
Level 2
November 25, 2022
Solved

Page not redirecting on form submit

  • November 25, 2022
  • 2 replies
  • 4024 views

Hi,

So have a core form and a custom action which calls a servlet when the user submits the form. I modified the action attribute in container.html on the core form container component to call on my servlet.

 

Here is a code snippet of my container.html

<form data-sly-use.container="com.el.core.models.impl.v1.form.FormContainerImpl"
data-sly-use.grid="com.day.cq.wcm.foundation.model.responsivegrid.ResponsiveGrid"
method="POST" action="${container.action @selectors='formsubmission'}" id="${container.id}" name="${container.name}"

Then my servlet looks like this. 

// package here

import lombok.extern.slf4j.Slf4j;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import java.io.IOException;

@Slf4j
@Component(service = Servlet.class, property = {
Constants.SERVICE_DESCRIPTION + "=Servlet for Form Submission",
"sling.servlet.methods=" + HttpConstants.METHOD_POST,
"sling.servlet.resourceTypes=" + FormSubmissionServlet.SERVLET_RESOURCE_TYPE,
"sling.servlet.selectors=" + "formsubmission",
"sling.servlet.extensions=" + "html"
})
public class FormSubmissionServlet extends SlingAllMethodsServlet {

static final String SERVLET_RESOURCE_TYPE = "sling/servlet/default";

private static final String REDIRECT_URL = ":redirect";

@Override
protected void doPost(final SlingHttpServletRequest request, final SlingHttpServletResponse response) throws ServletException, IOException {

String redirectUrl = request.getParameter(REDIRECT_URL);
// backend logic here

response.sendRedirect(redirectUrl);
}
}

 On the networks tab I can see that servlet is called but my thank you page is not loaded.

I can see also on the access.log the request. 

 

Any idea why it's not redirecting to the thank you page that i have set? 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SantoshSai

@santoshsai, I changed it to doGet() and it is redirecting now. My follow up question is why does redirect not work on post because one of the requirement is for me to set the method type to Post? Before I used servlet I used jsp to call my service and I used doPost on that but the redirect is working fine. What's the difference with servlet and service? 


@zmdeluyas 

Your POST request is being filtered and restricted by the “Apache Sling Referrer Filter” and “Adobe Granite CSRF Filter”. By default, the Apache Sling Referrer Filter blocks any incoming POST requests, and the Adobe Granite CSRF Filter blocks any incoming POST requests without the CSRF-Token token in the header.

You can solve this by following below steps

  • Allow incoming POST request in the Apache Sling Referrer Filter OSGI configurations, and
  • Remove the requirement of the CSRF-Token in the Adobe Granite CSRF Filter OSGI configurations.

Steps:

Configure Apache Sling Referrer Filter:

  1. Enable allow empty
  2. Remove the POST method from filters

In OSGI configurations (http://localhost:4502/system/console/configMgr), locate “Apache Sling Referrer Filter”. Enable the allow empty property, and remove the post method from filters property.

Configure Adobe Granite CSRF Filter

  1. Remove the POST method from filters

In OSGI configurations (http://localhost:4502/system/console/configMgr), locate “Adobe Granite CSRF Filter”. Remove the post method from filters property.

Note: After making configurations to the two OSGI configurations, you should be able to make a POST request from your HTTP REST Client to your AEM instance.

For production, set Apache Sling Referrer Filter and Adobe Granite CSRF Filter settings back to default. Unless if you are giving access to other servers to make POST requests to your AEM application.

Reference: https://sourcedcode.com/blog/video-tutorial/how-to-make-simple-http-post-methods-in-aem-with-a-http-rest-client 

2 replies

SantoshSai
Community Advisor
Community Advisor
November 25, 2022

Hi @zmdeluyas,

Can you let us know,

  • How/What did you set redirectUrl parameter in form?
  • What is the value in "redirectUrl"?

I can able to redirect it.

package com.mysite.core.servlets;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;

import javax.servlet.Servlet;
import java.io.IOException;
import java.rmi.ServerException;

@Component(service = Servlet.class, property = {Constants.SERVICE_DESCRIPTION + "=Simple Demo Redirect Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_POST, "sling.servlet.paths=" + "/bin/test/redirect"})

public class SimpleRedirectServlet extends SlingSafeMethodsServlet {

private static final long serialVersionUID = 2598426539166789516L;

@Override
protected void doGet(final SlingHttpServletRequest req, final SlingHttpServletResponse resp)
throws ServerException, IOException {
try {
resp.sendRedirect("/content/mysite/us/en.html");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Note: here I'm using GET (doGet) method instead doPost.

Hope that helps!

Regards,

Santosh

Santosh Sai
zmdeluyasAuthor
Level 2
November 25, 2022

This is the value of my redirect URL. 

 

SantoshSai
Community Advisor
Community Advisor
November 25, 2022

@zmdeluyas I can see in payload ":redirectis the actual page url which supposed to be passed in your code

 Can you try printing the variable redirectUrl

String redirectUrl = request.getParameter(REDIRECT_URL);

 and see what is the value you are getting.

Santosh Sai
Avinash_Gupta_
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 25, 2022

Hi @zmdeluyas With the above code snippet, I can't see that you have defined any page URL in the variable REDIRECT_URL.

 

Redirect URL set using the core forms container component will get saved as redirect property on form container node. You would have to get that the redirect url. Here is the code snippet that you can use in your servlet to get the value.

 

Resource formContainerResource = request.getResource();
ValueMap valueMap = formContainerResource.adaptTo(ValueMap.class);
String redirect = valueMap.get("redirect", String.class);
response.sendRedirect(redirect);

 

Hope this helps.

zmdeluyasAuthor
Level 2
November 25, 2022

I'm already getting the redirect. On the core container there's actually a hidden property there on the ootb for redirect url. I'm getting it via request parameters. Here is the more detailed image of my request. You can see the form data including the redirect. 

 

Avinash_Gupta_
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 25, 2022

@zmdeluyas As @santoshsai has suggested try printing the value of redirectUrl or debug this value. :redirect value is showing in the form data but it's value is not present on the request and that's the reason it would appear empty when you want to retrieve it's value from request parameter.

 

String redirectUrl = request.getParameter(REDIRECT_URL);