Jhontruse Jhontruse - 2 months ago 19
Ajax Question

Login ajax jsp servlet: Welcome page loading in div reserved for error message

I am developing a Login with jsp, servlet and ajax. When I enter the password incorrectly it shows the error message, but when I enter the correct key loads welcome.jsp into the div reserved for the error message.

From the servlet I send the variable to jsp. With javascript send the message to the div id="text"

Code JSP:

<body>
<script>
$(document).ready(function () {
$('#submitLogin').click(function (event) {
var nombreVar = $('#txtUsuario').val();
var apellidoVar = $('#txtPassword').val();

$.post('ServletLoguin', {
nombre: nombreVar,
apellido: apellidoVar

}, function (responseText) {

$('#texto').html(responseText);

});
});
});
</script>

<form id="formLogin" >
<label>ingrese usuario</label>
<input type="text" id="txtUsuario"><br> <br>
<label>ingrese clave</label>
<input type="password" id="txtPassword"><br><br>
<input type="button" id="submitLogin" value="Iniciar Sesion">
</form>
<div id="tabla">
<p id="texto" style="color:red;"></p>
</div>
</body>


Code Java servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out = response.getWriter();
String usuario = request.getParameter("nombre");
String password = request.getParameter("apellido");

try {
CallableStatement cl = Conexion.getConexion().prepareCall("{call SGVC_Login(?,?)}");
cl.setString(1, usuario);
cl.setString(2, password);
ResultSet rs = cl.executeQuery();
if (rs.next()) {

HttpSession sesionLoguin = request.getSession();
sesionLoguin.setAttribute("perfil", rs.getString(12));
sesionLoguin.setAttribute("nom", rs.getString(5));
sesionLoguin.setAttribute("apePaterno", rs.getString(3));
sesionLoguin.setAttribute("apeMaterno", rs.getString(4));
sesionLoguin.setAttribute("codUser", rs.getString(1));

if (sesionLoguin.getAttribute("perfil").equals("TVENTA") || sesionLoguin.getAttribute("perfil").equals("TCOBRANZA")) {

//request.getRequestDispatcher("principal1.jsp").forward(request, response);
response.sendRedirect("principal1.jsp");
//request.getRequestDispatcher("ServletRegistrarVenta.java").forward(request, response);

} else {
//request.getRequestDispatcher("principal2.jsp").forward(request, response);
response.sendRedirect("principal2.jsp");
}

} else {
response.setContentType("text/html; charset=iso-8859-1");
out.println("error");

}

} catch (Exception e) {
System.err.println("error " + e);
}

}

Answer Source

Since you are using ajax, it will not reload your page. So either you can use form.submit() or from your ajax call back method use window.location.href = '<your_page>.jsp' after passing success as response like

if(reponseText == 'principal1')
    window.location.href = 'principal1.jsp';
else if(reponseText == 'principal2')
    window.location.href = 'principal2.jsp';
else
    $('#texto').html(responseText);

>

protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {       

PrintWriter out = response.getWriter();
String usuario = request.getParameter("nombre");
String password = request.getParameter("apellido");

try {
    CallableStatement cl = Conexion.getConexion().prepareCall("{call SGVC_Login(?,?)}");
    cl.setString(1, usuario);
    cl.setString(2, password);
    ResultSet rs = cl.executeQuery();
    if (rs.next()) {

        HttpSession sesionLoguin = request.getSession();
        sesionLoguin.setAttribute("perfil", rs.getString(12));
        sesionLoguin.setAttribute("nom", rs.getString(5));
        sesionLoguin.setAttribute("apePaterno", rs.getString(3));
        sesionLoguin.setAttribute("apeMaterno", rs.getString(4));
        sesionLoguin.setAttribute("codUser", rs.getString(1));

        if (sesionLoguin.getAttribute("perfil").equals("TVENTA") || sesionLoguin.getAttribute("perfil").equals("TCOBRANZA")) {

            response.setContentType("text/html; charset=iso-8859-1");
            out.write("principal1");         

        } else {
            response.setContentType("text/html; charset=iso-8859-1");
            out.write("principal2");
        }

    } else {
        response.setContentType("text/html; charset=iso-8859-1");
        out.write("error");

    }

} catch (Exception e) {
    System.err.println("error " + e);
}       

}