SlingPostServlet not redirecting to new HTML

Avatar

Avatar

sukumard5750513

Avatar

sukumard5750513

sukumard5750513

02-09-2019

Hi,

I am trying to post some data via a component to a servlet and then redirecting to another page. I see a new GET request with 200 status in network tab but the browser is not loading with new page , but loads with same page by appending the post data as query param.

Below is my piece of code

Component JSP  to submit data :-

<%@include file="/libs/foundation/global.jsp"%>

<cq:includeClientLib categories="cq.jquery" />

<html>

<head>

<meta charset="UTF-8">

<title>Form Submit Page</title>

<script>

$(document).ready(function() {

$('#submit').click(function() {

    var failure = function(err) {

             alert("Unable to retrive data "+err);

   };

    debugger;

    var flowId= $('#flowId').val() ;

    $.ajax({

         type: 'POST',   

        url:'/bin/formSubmit',

         data:'flowId='+ flowId,

         success: function(msg){

        alert("Response");

         }

     });

  });

    

});

</script>

</head>

<title>Form Submit</title>

<body>

<h1>Form Submit</h1>

</div>

         

<form method="#" >

        <label for="flowId">flowId</labe> <input id="flowId" name="flowId"> </input>

       

                <button id="submit" type="submit">Submit</button>

    </form>

</body> 

</html>

Servlet :-

package com.aem.test.core.servlets;

import static org.apache.sling.api.servlets.ServletResolverConstants.SLING_SERVLET_PATHS;

import java.io.IOException;

import java.util.UUID;

import javax.servlet.RequestDispatcher;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import javax.servlet.http.HttpSession;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.api.servlets.HttpConstants;

import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.apache.sling.jcr.api.SlingRepository;

import org.osgi.framework.Constants;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

@SuppressWarnings("deprecation")

@Component(service=Servlet.class,

           property={

                   Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",

                   "sling.servlet.methods=" + HttpConstants.METHOD_POST,

                   SLING_SERVLET_PATHS + "=/bin/formSubmit",

                   "sling.servlet.extensions=" + "html"

           })

public class PostFormAndReturnServlet extends SlingAllMethodsServlet {

    private static final long serialVersionUID = 1L;

    private static final Logger LOGGER = LoggerFactory.getLogger(PostFormAndReturnServlet.class);

   

    @Reference

    private SlingRepository repository;

    

    public void bindRepository(SlingRepository repository) {

           this.repository = repository;

           }

   

    @Override

    protected void doPost(final SlingHttpServletRequest request,

            final SlingHttpServletResponse response) throws ServletException, IOException {

    LOGGER.info("Coming to POST");

        response.setContentType("text/html");

response.setCharacterEncoding("UTF-8");

String recoveryFlowId = request.getParameter("flowId");

LOGGER.info("Coming to POST ::recoveryFlowId ::"+recoveryFlowId);

response.sendRedirect("/content/AEM63App/en/ResourceTypePage.html");

    }

}

When i submit the form :- http://localhost:4504/content/AEM63App/en/SubmitForm.html

In network i can see 200 ok request to /content/AEM63App/en/ResourceTypePage.html but page loads with

http://localhost:4504/content/AEM63App/en/SubmitForm.html?flowId=zzz

screenshot before send redirect :-

1825975_pastedImage_18.png

Screenshot After redirect :-

1825974_pastedImage_17.png

Please help.

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

sukumard5750513

Avatar

sukumard5750513

sukumard5750513

02-09-2019

I found the solution. I just modified the component code to post the data as below & it started working.

<form method="post" action="/bin/formSubmit" >

        <label for="flowId">flowId</labe> <input id="flowId" name="flowId"> </input>

      

                <button id="submit" type="submit">Submit</button>

    </form>

Answers (0)