I have a login jsp, and a servlet that processes the login. The servlet is called by jquery ajax. If the email or password is incorrect, a message from the servlet will be displayed in a div.
response.getWriter().write("Incorrect");
But if it is correct, the servlet calls:
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print("{\"redirect:\",\"homepage.jsp\"}");
I followed the steps here How to manage a redirect request after a jQuery Ajax call to manage response.sendRedirect calls by the servlet. I got it to display the message if the email/password is incorrect, but it's not redirecting, homepage.jsp got included in the login page. Here's the ajax.
$( "#login" ).submit(function( event ) {
event.preventDefault();
$.post(
$(this).attr( "action" ),
{ email: $(this).find( "input[name='email']" ).val(),
password: $(this).find( "input[name='password']" ).val()})
.done(function( data, textStatus) {
if (data.redirect) {
window.location.href = data.redirect;
} else {
$( "#message" ).empty().append( data ); // display to a div
}
},"json");
});
What should I do?