Expand my Community achievements bar.

RequestDispatcher and sendRedirect not working.

Avatar

Level 2

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

9 Replies

Avatar

Community Advisor

Hi,

Can you share your code?

Please check Apache Sling :: Dispatching Requests



Arun Patidar

Avatar

Level 2

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

}*/

}

}

Avatar

Community Advisor

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 ?



Arun Patidar

Avatar

Level 2

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

Avatar

Community Advisor

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



Arun Patidar

Avatar

Level 2

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

Avatar

Community Advisor

Hi,

Can you check error.log?

did you try servlet which I created?



Arun Patidar

Avatar

Level 2

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