Highlighted

RequestDispatcher and sendRedirect not working.

Avatar

Avatar

karthikbhat3624

Avatar

karthikbhat3624

karthikbhat3624

18-09-2018

Hello,

In Servlet I'm trying to redirect to another page using both RequestDispatcher and sendRedirect bu t bith are seem to be not working.

What can I do?

Thank You

AEM - Questions

Replies

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

18-09-2018

Hi,

Can you share your code?

Please check Apache Sling :: Dispatching Requests

Highlighted

Avatar

Avatar

karthikbhat3624

Avatar

karthikbhat3624

karthikbhat3624

18-09-2018

package com.mindtree.travellingdiva.core.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import javax.servlet.http.HttpSession;

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.SlingAllMethodsServlet;

import org.osgi.framework.Constants;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import com.mindtree.travellingdiva.core.dbservice.TDService;

@Component(service=Servlet.class,

property={

Constants.SERVICE_DESCRIPTION+"= Header Servlet",

"sling.servlet.methods=" + HttpConstants.METHOD_POST,

"sling.servlet.paths=/bin/LoginServlet",

"sling.servlet.resourceTypes="+"travellingdiva/components/structure/login",

"sling.servlet.extensions="+"txt"

})

public class LoginServlet extends SlingAllMethodsServlet{

/**

*

*/

private static final long serialVersionUID = 1L;

@Reference

TDService service;

@Override

protected void doPost(final SlingHttpServletRequest req, final SlingHttpServletResponse res) throws IOException

{

String username = req.getParameter("uname");

String password = req.getParameter("pass");

String st;

boolean result = service.verifyUser(username,password);

if(result==true)

{

HttpSession session = req.getSession();

st=""+session.getAttribute("username")+"  ";

st=st+"User Exist  ";

session.setAttribute("username", username);

session.setMaxInactiveInterval(1800);

st=st+session.getAttribute("username");

}

else

st="Invalid username/password";

//res.getWriter().write(st);

res.sendRedirect("/content/travellingdiva/project/home.html");

//RequestDispatcher rd = req.getRequestDispatcher("/content/travellingdiva/project/home.html");

/*try {

rd.include(req, res);

} catch (ServletException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}*/

}

}

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

18-09-2018

Hi,

I created a demo servlet like yours and it is working as expected and redirecting to target page when I hit http://localhost:4502/bin/LoginServlet.txt

aem63app-repo/LoginServletRedirect.java at master · arunpatidar02/aem63app-repo · GitHub

Can try to same or check your code where it is failing ?

Avatar

Avatar

karthikbhat3624

Avatar

karthikbhat3624

karthikbhat3624

18-09-2018

For me Session is being set but I'm not able to call the Website, even it is not giving any exceptions,

Highlighted

Avatar

Avatar

karthikbhat3624

Avatar

karthikbhat3624

karthikbhat3624

18-09-2018

It is Staying in same page

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

18-09-2018

Can you try to put some logger before below code and check if code is executed to not?

res.sendRedirect("/content/travellingdiva/project/home.html");

Highlighted

Avatar

Avatar

karthikbhat3624

Avatar

karthikbhat3624

karthikbhat3624

18-09-2018

I've added, It is logged in the log file file, but still page has not changed.

Highlighted

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

18-09-2018

Hi,

Can you check error.log?

did you try servlet which I created?

Highlighted

Avatar

Avatar

karthikbhat3624

Avatar

karthikbhat3624

karthikbhat3624

18-09-2018

I've tried but no luck...Thank you for your valuable time...