I'm doing a data maintainer in jsp, so for the user validation part I use servelts to make calls to mysql.
In the servelts of consultas.java I have this (it is to do reedireccion 1 is admin 2 is standard):
public boolean comprobar (String usuario,String contraseña){
PreparedStatement pst = null;
ResultSet rs =null;
try {
String sql = "Select nivel from usuarios where usuario = ? and password =?";
pst = getconexion().prepareStatement(sql);
pst.setString(1, usuario);
pst.setString(2, contraseña);
rs = pst.executeQuery();
if (rs.next()) {
int id = rs.getInt("nivel");
System.out.print(id);
if (id == 1) {
return true;
}else{
return false;
}
}
} catch (Exception e) {
System.err.println("Error comprobar"+e);
}
return false;
}
Then in the servelt of login.java I have one to validate the existing user which does the same thing returns true or false, and inside I introduce the user level checking
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String usuario = request.getParameter("txt_user");
String contraseña = request.getParameter("txt_pass");
consultas co = new consultas();
if (co.autenticacion(usuario, contraseña)) {//Este da true sin dramas
if (co.comprobar(usuario, contraseña)) {//Este deberia dar true desde consultas.java
response.sendRedirect("entrada_admin.jsp");
}else{ //Pero siempre termina aca sin importar el usuario -__-
response.sendRedirect("entrada_estandar.jsp");
}
}else{
response.sendRedirect("index.jsp");
}
}
As mentioned, the code does not send me where I want. I have tried checking with this code inside queries and it returns true and false without problems.
public static void main(String[] args) {
consultas co = new consultas();
System.out.println(co.comprobar("usuario", "contraseña"));
}