Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

redirect to a content page in aem servlet

Avatar

Level 2

I was trying to redirect to another content page in aem servlet. It is returning 200 OK response in network tab but NEVER goes to the specified redirect page.

Is this how AEM works, if so, Do I need to make any changes in configurations or any other code changes to get this redirect work as in a regular servlet application.

I appreciate any help.

I tried both redirect and forward as you can see in below code. In case of forward I am getting 500 error in network tab.

@Component(immediate = true, service = Servlet.class, property = { "sling.servlet.paths=/bin/test/redirect"})

public class TestRedirectServlet extends SlingAllMethodsServlet{

   

private static final long serialVersionUID = 3591693830449607948L;

   

@Override

protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)  {

        PrintWriter writer = null;

        try {

            writer = response.getWriter();       

      //       final RequestDispatcherOptions options = new RequestDispatcherOptions();

      //       final SlingHttpServletRequest syntheticRequest = new SyntheticSlingHttpServletGetRequest(request);

      //       request.getRequestDispatcher("/content/best-western/en_US.html", options).forward(syntheticRequest, response);

      //       return;

      

         response.sendRedirect("/content/test-site/sample.html");       

            }

        } catch(Exception e){

      

       }

        finally {

            if (writer != null) {

                writer.print("done");

                writer.flush();

                writer.close();

            }

        }

}

6 Replies

Avatar

Community Advisor

below is working for me

import java.io.IOException;

import java.rmi.ServerException;

import javax.servlet.Servlet;

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;

@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/we-retail/us/en/user.html");

} catch (Exception e) {

e.printStackTrace();

}

}

}

redirect.PNG



Arun Patidar

Avatar

Level 2

Hi Arun,

Thanks for your reply.

Does your page load with user.html page and browser url also have /content/we-retail/us/en/user.html in url locator.

I am using AEM 6.3.3 version.

Thanks,

Sai

Avatar

Community Advisor

Yes, I am redirecting so the url changed from /bin/test/redirect to /content/we-retail/us/en/user.html



Arun Patidar

Avatar

Level 2

Are you calling this servlet from javascript ajax ?

Avatar

Level 4

Hi Sai,

Could you please let us know how you are trying to access?

As arun suggested it should work whether you are calling through script or browser once your servlet is called it should redirect to respective path.

Avatar

Level 2

Are you trying to achieve this from a button in Granite UI dialog?