redirect to a content page in aem servlet | Community
Skip to main content
sai_nagabethini
Level 2
May 10, 2019

redirect to a content page in aem servlet

  • May 10, 2019
  • 3 replies
  • 11203 views

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();

            }

        }

}

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

3 replies

arunpatidar
Community Advisor
Community Advisor
May 10, 2019

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();

}

}

}

Arun Patidar
sai_nagabethini
Level 2
May 10, 2019

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

arunpatidar
Community Advisor
Community Advisor
May 10, 2019

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

Arun Patidar
Level 4
May 10, 2019

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.

Level 2
September 16, 2019

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