i'm trying to forward to another page after authentication. If i call the servlet with a form with an action to servlet: it works. But, when i call it with jquery funtion doesn't do anything. (the authentixùcation is correct, i checked it with debug and return the correct value of by user)
@WebServlet(name = "Controller", value = "/controller-servlet")
public class Controller extends HttpServlet {
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void destroy() {
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html; charset=UTF-8");
ServletContext ctx = getServletContext();
RequestDispatcher rd = ctx.getNamedDispatcher("ValidateServlet");
rd.include(request,response);
HttpSession s = request.getSession();
String role = (String) s.getAttribute("Ruolo");
if(role.equals("Studente")){
rd = ctx.getRequestDispatcher("/student.html");
}else if(role.equals("Docente")){
rd = ctx.getRequestDispatcher("/docente.html");
}
else{
PrintWriter out = response.getWriter();
out.println("NOT IN DB");
}
rd.forward(request, response);
}
}
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Pagina di identificazione</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Identificazione utente</h2>
<form method="post">
<p> Login: <input id="mail" type="text" name="login" value=""/> </p>
<p>Password: <input id="pass" type="password" name="passwd"/></p>
<p><input id="login" type="button" name="submit" value="login"/></p>
</form>
<div id="risposta" class="outputTextArea"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#login").click(function (){
var account = document.getElementById("mail").value;
var password = document.getElementById("pass").value;
$.post("controller-servlet",{mail: account,passwd: password},
function (data){
$("risposta").html(data);
});
//$("#insert").load("insert.html");
});
});
</script>
</body>
</html>