Having trouble with page redirection from Servlet despite hours of searching for a solution. Any help would be greatly appreciated. Here's the JavaScript code:
$("#login_form_submit").click(function(){
var form = $('#login_form');
$.ajax({
type: "Post",
url: "AccountServlet",
data: form.serialize(),
dataType: "json",
//if received a response from the server
success: function( data, textStatus, jqXHR) {
if(data.success){
$("#ajaxResponse").html("");
console.log(data);
if (data.User.role == 1){
main.user = data.User;
//window.location.href = "Student_home.jsp";
}
}
//display error message
else {
$("#ajaxResponse").html("<div><b>Something goes wrong check your email or password</b></div>");
}
},
//If there was no response from the server
error: function(jqXHR, textStatus, errorThrown){
$("#ajaxResponse").html("<div><b>Something goes wrong check your email or password</b></div>");
$("#ajaxResponse").html(jqXHR.responseText);
},
//capture the request before it was sent to server
beforeSend: function(jqXHR, settings){
//disable the button until we get the response
$('#login_form_submit').attr("disabled", true);
},
//this is called after the response or error functions are finsihed
//so that we can take some action
complete: function(jqXHR, textStatus){
//enable the button
$('#login_form_submit').attr("disabled", false);
}
});
});
And here's the servlet code:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
response.setHeader("Cache-control", "no-cache, no-store");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "-1");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
response.setHeader("Access-Control-Max-Age", "86400");
String userName = request.getParameter("userName");
String password = request.getParameter("password");
DatabaseWrapper context = new DatabaseWrapper();
User user = context.Check_login(userName, password);
session.current_user = user;
Gson gson = new Gson();
JsonObject myObj = new JsonObject();
if (user == null){
myObj.addProperty("success", false);
out.println(myObj.toString());
}else if (user instanceof Student){
JsonElement userObject = gson.toJsonTree((Student)user);
myObj.addProperty("success", true);
myObj.add("User", userObject);
out.println(myObj.toString());
out.close();
//response.setStatus(HttpServletResponse.SC_FOUND);
response.sendRedirect("Student_home.jsp");
return;
}
I also tried using this piece of code:
RequestDispatcher dispatcher = request.getRequestDispatcher("/Student_home.jsp");
dispatcher.forward(request, response);
but it didn't work. Any suggestions?
I prefer using only JavaScript for now, but I need to redirect from the servlet!
I attempted to use this line in JavaScript:
window.location.href = "Student_home.jsp";
Found the solution but couldn't post it as an answer. The issue lies in redirecting the Ajax call without preventing it. The workaround involves creating a form element in the jQuery success response and submitting it naturally:
function Redirect (servlet , method_type){
var form = $(document.createElement('form'));
$(form).attr("action", servlet);
$(form).attr("method", method_type);
$(form).css("display", "none");
form.appendTo( document.body );
$(form).submit();
}
Problem solved, thanks everyone!