4 Replies Latest reply on Dec 2, 2012 10:12 PM by LingeshP

    Processing Form Data

    LingeshP Level 1

      Hi,

       

      I'm new to CQ and I'm trying to achieve the user registration and login functionality for a website.

       

      Below are the questions for which I'm looking for clarifications

       

      1. How do we process the user entered data upon submission? Since we are processing the form data against the external Database/Webservice, Is there any generic approach that I can follow in CQ to achieve this?

       

      Expected functionality,

       

      1. user fills the form(any form)

      2. upon submission process the form data

      3. Based on the processing, redirect to result page.

       

      Request your suggestions on this.

       

      Thanks in advance.

       

      Regards,

      Lingesh P.

        • 1. Re: Processing Form Data
          rush_pawan Level 4

          Hello Lingesh,

           

          I would prefer you to refer http://dev.day.com/docs/en/cq/current/developing/developing-forms.html which help you to know how you can use existing OOTB form component and various configuration options.

           

          Now once you done with above then you can configure the form to use custom servlet by providing custom servlet path in "Action Configuration (Content path)" inside "Advance tab"

           

          Servlet will help you to do process on form submission as you mentioned above. How you can develop servlet refer my earlier post for example - http://forums.adobe.com/message/4830914#4830914

           

          from above example you can call servlet using "/libs/testservlet.json" same can set in (

          "Action Configuration (Content path)" inside "Advance tab" as mentioned above)

           

          I hope it will help you proceed. Let me know for more information or above doesn't help you.

           

           

          Thanks,

          Pawan

          1 person found this helpful
          • 2. Re: Processing Form Data
            LingeshP Level 1

            Hi Pawan,

             

            Thanks for your inputs !.

             

            I just tried the option you have suggested and done the following.

            1. Created a custom form action component and able to list in the action type.

            2. Included forward.jsp, for now the path is just hardcoded

                FormsHelper.setForwardPath(slingRequest, "/libs/TestServlet.html");    

                FormsHelper.setRedirectToReferrer(request, true);

            3. Created two servlets TestServlet and SampleServlet, in which TestServlet extend SlingAllMethodsServlet and SampleServlet extends the HttpServlet. Generated bundle out of this

            4. For testing both the servlets will print some logger and redirects to another page using sendRedirect method.

            5. In the form components, i have configured /libs/TestServlet.html in the advanced tab. And when I submit the form it shows the below error. I was not clear about this issue and hence tried point 6

             

                 Not a Sling HTTP request/response

                        Cannot serve request to /content/formpage.html in com.kpn.login.testproject.TestServlet

                       Exception:

                 javax.servlet.ServletException: Not a Sling HTTP request/response

                    at      org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.jav a:374)

                    at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:500)

                    at      org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilter Chain.java:45)

            6. Created a SampleServlet which extend HttpServlet and cofigured /libs/SampleServlet.html in the cocntent path. With this configuration I'm able to render the form data and redirection works fine.

             

            However I'm not able to understand what went wrong when I used TestServlet. Out of these two which servlet is preferred?

             

            Also,I have a question on the action configuration.

            1. Is it mandatory to have forward.jsp in the action component even when we set the cotent path(servlet path) in the advanced tab? How this works?

             

            Thanks in advance.

             

            Regards,

            Lingesh P.

            • 3. Re: Processing Form Data
              rush_pawan Level 4

              Hi,

               

              Please check whether you can access TestServlet directly through HTTP call or not means just call http://localhost:4502/libs/TestServlet.html if you get response (200) means the servlet is deployed and visible through sling servlet registry. if you get error then plz check how you have configured it also plz check @Property(name = "sling.servlet.paths", value = "<servlet path >"), how you have configured it.

               

              Answere your other questions

               

              1. Using sling servlet is correct option but for form also dont forget to override doPost method as from form you will generate post call.

              2. I think as per your requirement you need not required forward.jsp to override because that you can do through your servlet any how, so try to handle it from there only.

               

              I hope it helps you for more information let me know.

               

               

              Thanks,

              Pawan

              • 4. Re: Processing Form Data
                LingeshP Level 1

                Hi Pawan,

                 

                I just checked the configuration in the sling servlet file. PFB the TestServlet for your reference.

                 

                With this configuration I'm still getting "Not a sling HTTP request/response" error.

                 

                package com.kpn.login.testproject;

                 

                import java.io.IOException;

                import java.io.Serializable;

                 

                import javax.servlet.Servlet;

                import javax.servlet.ServletException;

                 

                import org.apache.sling.api.SlingHttpServletRequest;

                import org.apache.sling.api.SlingHttpServletResponse;

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

                 

                import org.slf4j.Logger;

                import org.slf4j.LoggerFactory;

                import org.apache.felix.scr.annotations.Component;

                 

                import org.apache.felix.scr.annotations.Properties;

                 

                import org.apache.felix.scr.annotations.Property;

                 

                import org.apache.felix.scr.annotations.Service;

                @Service(value = Servlet.class)

                 

                @Component(immediate = true, metatype = true)

                 

                @Properties( {

                 

                        @Property(name = "sling.servlet.paths", value = "/libs/TestServlet.html"),

                 

                        @Property(name = "service.description", value = "abcd"),

                 

                        @Property(name = "label", value = "TestServlet") })

                 

                 

                 

                public class TestServlet extends SlingAllMethodsServlet implements Serializable{

                /**

                     *

                     */

                    private static final long serialVersionUID = 1L;

                   

                protected void doGet(SlingHttpServletRequest request,

                        SlingHttpServletResponse response) throws ServletException,

                        IOException {

                   

                    final Logger logger = LoggerFactory.getLogger(TestServlet.class);

                    logger.info("Inside Servlet");

                    response.sendRedirect("/content/testpage.html");

                   

                }

                @Override

                    protected void doPost(SlingHttpServletRequest request,

                            SlingHttpServletResponse response) throws ServletException,

                            IOException {

                        doGet(request, response);

                    }

                }