redirect to a content page in aem servlet

Avatar

Avatar
Validate 1
Level 1
sai_nagabethini
Level 1

Likes

0 likes

Total Posts

6 posts

Correct reply

0 solutions
Top badges earned
Validate 1
View profile

Avatar
Validate 1
Level 1
sai_nagabethini
Level 1

Likes

0 likes

Total Posts

6 posts

Correct reply

0 solutions
Top badges earned
Validate 1
View profile
sai_nagabethini
Level 1

09-05-2019

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

            }

        }

}

Replies

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,322 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,322 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

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

}

}

}

redirect.PNG

Avatar

Avatar
Validate 1
Level 1
sai_nagabethini
Level 1

Likes

0 likes

Total Posts

6 posts

Correct reply

0 solutions
Top badges earned
Validate 1
View profile

Avatar
Validate 1
Level 1
sai_nagabethini
Level 1

Likes

0 likes

Total Posts

6 posts

Correct reply

0 solutions
Top badges earned
Validate 1
View profile
sai_nagabethini
Level 1

10-05-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

Avatar

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,322 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile

Avatar
Coach
MVP
Arun_Patidar
MVP

Likes

1,442 likes

Total Posts

3,322 posts

Correct reply

941 solutions
Top badges earned
Coach
Contributor 2
Ignite 10
Give Back 700
Boost 1000
View profile
Arun_Patidar
MVP

10-05-2019

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

Avatar

Avatar
Validate 1
Level 1
sai_nagabethini
Level 1

Likes

0 likes

Total Posts

6 posts

Correct reply

0 solutions
Top badges earned
Validate 1
View profile

Avatar
Validate 1
Level 1
sai_nagabethini
Level 1

Likes

0 likes

Total Posts

6 posts

Correct reply

0 solutions
Top badges earned
Validate 1
View profile
sai_nagabethini
Level 1

10-05-2019

Are you calling this servlet from javascript ajax ?

Avatar

Avatar
Validate 10
Level 3
ankurk67503819
Level 3

Likes

15 likes

Total Posts

89 posts

Correct reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 10
Boost 5
Boost 3
View profile

Avatar
Validate 10
Level 3
ankurk67503819
Level 3

Likes

15 likes

Total Posts

89 posts

Correct reply

1 solution
Top badges earned
Validate 10
Validate 1
Boost 10
Boost 5
Boost 3
View profile
ankurk67503819
Level 3

10-05-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.

Avatar

Avatar
Validate 1
Level 2
Ashish_Ranjan1
Level 2

Likes

3 likes

Total Posts

16 posts

Correct reply

2 solutions
Top badges earned
Validate 1
Ignite 1
Give Back
Boost 3
Boost 1
View profile

Avatar
Validate 1
Level 2
Ashish_Ranjan1
Level 2

Likes

3 likes

Total Posts

16 posts

Correct reply

2 solutions
Top badges earned
Validate 1
Ignite 1
Give Back
Boost 3
Boost 1
View profile
Ashish_Ranjan1
Level 2

16-09-2019

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