2 Replies Latest reply on Dec 20, 2012 4:02 AM by div_sgh

    redirect from one jsp to another jsp

    div_sgh Level 1

      Hi,

      I am very new to CQ5 and have a query regarding it. Suppose I want to make a simple login form where I am two text fields.. I want to perform some validations on these text fields and want to redirect to some aonther jsp/html. How to achieve this small requirement in cq5.

       

      Please explain the process in detail as I am very new to the env and doing some hands on the env.

       

      Thanks.,

        • 1. Re: redirect from one jsp to another jsp
          kasq Level 3

          Hi,

           

          In case of this simple form, you write a simple Sling Post Servlet. More details how to start you find checking following link:

           

          http://sling.apache.org/site/manipulating-content-the-slingpostservlet-servletspost.html

           

          and in the action define a path to this servlet which will be recognize by the selector name defined in the servlet.

           

          This is simple example of the form:

           

          <form method="POST" action="/example.SELECTORNAME.html">

              <input type="text" name="variable1" value="" />

              <input type="text" name="variable2" value="" />

             <input type="hidden" name=":redirect" value="ANOTHER HTML PAGE" />

             <input type="Submit" />

          </form>

           

          and simple Sling Post Servlet which must be deployed in the Apache Felix Console in the bundle:

           

          package com.example;

           

          import javax.servlet.ServletException;

           

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

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

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

          import org.apache.sling.api.SlingHttpServletRequest;

          import org.apache.sling.api.SlingHttpServletResponse;

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

           

          @Component(immediate=true, metatype=false, label="EXAMPLE SERVLET")

          @Service

          @Properties(value = {

              @org.apache.felix.scr.annotations.Property(name="sling.servlet.methods", value={"POST"}),

              @org.apache.felix.scr.annotations.Property(name="sling.servlet.resourceTypes", value={"sling/servlet/default"}),

              @org.apache.felix.scr.annotations.Property(name="sling.servlet.selectors", value={"SELECTORNAME"}),

              @org.apache.felix.scr.annotations.Property(name="sling.servlet.extensions", value={"html"})

          })

           

          public class ExampleServlet extends SlingAllMethodsServlet {

           

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

                  String redirect = request.getParameter(":redirect");

                  // SOME LOGIC

                  response.sendRedirect(redirect);

          }

           

          }

           

          In case of validation it depends on the requirements. You can use client side validation (java script function) and then:

           

          - if validation will be ok submit form to the servlet which redirects to another html page or

          - if validation fails form will not be submitted and some error messages will be shown

           

          or you can create server side validation and depends on it redirect to the same page with a form with some error messages in the json format  or to other error page

           

          Of course this are basics to show only the main idea hwo you can implement this kind of stuff.

           

          I hope that this helps you in some way.

           

          Regards,

          kasq

          • 2. Re: redirect from one jsp to another jsp
            div_sgh Level 1

            Thanks a lot for your help... It was of good help