0
I am trying to validate the login and password of the user, in which if you do not enter the login and password fields a message will appear on the screen "Please fill in the login fields or password" or if the user enters the incorrect user or password a message will appear on the screen "Incorrect login or password" but it does not show messages.
DAO class
public Cliente validacaoLogin(String login,String senha){
Cliente cli = new Cliente();
try {
con = Conecta.conexao();
} catch (ClassNotFoundException ex) {
Logger.getLogger(ClienteDAO.class.getName()).log(Level.SEVERE, null, ex);
}
//String sql="Select cli_nome from tb_cliente where cli_login=? and cli_senha=? ";
String sql="Select cli_nome,cli_login,cli_senha from tb_cliente where cli_login= '" + login + "' and cli_senha= '" + senha + "'";
try {
pst = con.prepareStatement(sql);
rs= pst.executeQuery();
while(rs.next()){
cli.setLogin(rs.getString("cli_login"));
cli.setSenha(rs.getString("cli_senha"));
cli.setNome(rs.getString("cli_nome"));
}
return cli;
} catch (Exception e) {
JOptionPane.showConfirmDialog(null,e);
return null;
}
}
Servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);
String vazio="Por favor preencha os campos de login ou senha";
String incorretos="Login ou senha incorretos";
String login = request.getParameter("login");
String senha = request.getParameter("senha");
Cliente cli= new Cliente();
LoginDAO login2= new LoginDAO();
Cliente login_validacao=login2.validacaoLogin(login,senha);
if(login_validacao.getLogin()==null && login_validacao.getSenha()==null){
request.setAttribute("vazio", vazio);
}
else if(login_validacao.getLogin().equals(request.getParameter("login")) & login_validacao.getSenha().equals((request.getParameter("senha")))){
RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");
rd.forward(request, response);
}
else{
request.setAttribute("incorreto", incorretos);
}
}
Page JSP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="js/jquery-1.12.0.min.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput.min.js" type="text/javascript">
</script>
<script src="bootstrap/bootstrap.min.js" type="text/javascript">
</script>
<script src="bootstrap/bootstrap.js" type="text/javascript"></script>
<link rel="stylesheet" href="bootstrap/bootstrap.css">
<link rel="stylesheet" href="css/css.css" type="text/css">
<link rel="stylesheet" href="css/font-awesome.min.css" type="text/css"/>
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-
awesome.min.css" rel="stylesheet">
<title>JSP Page</title>
</head>
<body>
<div class="login-form">
<div class="row">
<div class="col-6 col-md-4"></div>
<div class="col-6 col-md-4">
<h2 id="AcesseConta"><span>Acesse sua Conta</span></h2>
<label for="vazio_resposta">${vazio.vazio}</label>
<label for="validacao">${incorreto.incorretos}</label>
</div>
</div>
<div class="row">
<div class="col-6 col-md-4"></div>
<div class="col-6 col-md-4">
Login:<input type="text" id="login" name="login" ><br/><br/>
Senha:<input type="text" id="senha" name="senha" ><br/>
<a href="">Não é cadastrado?Cadastre-se</a><br/><br/>
</div>
</div>
<div class="row">
<div class="col-6 col-md-4"></div>
<div class="col-6 col-md-4">
<div id="teste"></div>
<button type="button" onclick="login()" >Entrar</button> <button
type="button" onclick="" >Esqueceu a Senha?</button>
</div>
</div>
</div>
</body>
i place the Response.setAttribute("empty", empty); on my Servlet but it displays the following error Cann not find Symbol method setAttribute
– User1999
Good afternoon. actually setAttribute is a method from Httpservletrequest and not from Httpservletresponse. tries to use Response.setHeader("empty", empty);
– Isaias Buscarino
Please also take a look at http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html and http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html
– Isaias Buscarino