Java web, jstl :How to execute a method in Servlet when exiting an input text ex:"check whether the name availability for registration"

Asked

Viewed 62 times

1

How to execute a method when exiting a input text. Example I will make a registration at the time of registering as a user I want to check if that name is available to be used , and can not accept repeat users, to avoid that the check is at the time of sending all the data would like to do this check when the user finishes typing and pass to the next field to fill.

I’ve already done the Servlet and the method DAO , but the part of jsp I’m kind of lost I see a lot of people talking about ajax, but would like to avoid using it.

My brother

if(funcao.equals("verificaUsuario")){
    String usuario = request.getParameter("user");
    boolean verifica = log.verificarUsuario(usuario);

    if(verifica = true){
        request.setAttribute("O nome '"+usuario, "' este indisponível para uso, por gentileza tente outro nome para realizar o cadastro." );                
    }else{
        request.setAttribute("Prabéns! O nome '"+usuario, "' este disponível para uso." );                                
    }

    RequestDispatcher rd = request.getRequestDispatcher("v_login_cad.jsp");                
    rd.forward(request, response);   
}

my DAO

public boolean verificarUsuario(String nome) {
    String sql = "select * from login where log_nome=? ";
    try {
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, nome);            
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            return true;
        } else {
            return false;
        }
    } catch (SQLException e) {
        //throw new RuntimeException("Erro 1_D   " + e);
        Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null,"Erro 2_D  " + e);
    }
    return false;
}
  • I don’t think you can do this safely without using Javascript. In case you create an endpoint to receive the data from the field, in the focusout of that field do an asynchronous ajax that sends the data to your endpoint validate and return a boolean.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.