找到你要的答案

Q:Instant username check with java bean and jquery remote validators

Q:即时的用户名检查java bean和jQuery远程验证器

Hi guys i have registration form designed in bootstrap where i have validate all the form fileds using remote validator and i have registerdao.java method which has checkusername boolean method which checks all the rows and if username exists it returns username string otherwise false and i have checkusernameexists method in registartiion servlet which checks username when you enter username fields.I don't understand why my code is not showing anything.Below is the code is not checking username against database and notifying.How to write javascript so that it checks the username against data or give me some ideas how to make this code work? any help would be greatly appreaciated?thanks

ReegisterDao.java public static boolean checkUserName(String userName) throws SQLException{ String checkUserNameQuery = "select * from EmployeeDetails where USER_NAME='"+userName+"'";

  try{


 // preparing some objects for connection
currentCon = ConnectionManager.getConnection();
Statement stmt = currentCon.createStatement();
rs  = stmt.executeQuery(checkUserNameQuery);
boolean more = rs.next();

    // if user does not exist set the isValid variable to false
    return more;
     }



catch (InstantiationException | IllegalAccessException e){}

return false; }

RegistrationServlet.java

public int checkUserNameExists(RegistrationBean user) throws SQLException{

        int a=RegistrationDao.Register(user);


    return a;

}

this is part of remote validator which validates username

   username: {
                    validators: {
                        notEmpty: {
                            message: 'The username is required'
                        },
                        stringLength: {
                            min: 6,
                            max: 30,
                            message: 'The username must be more than 6 and less than 30 characters long'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9_\.]+$/,
                            message: 'The username can only consist of alphabetical, number, dot and underscore'
                        },
                        remote:{
                            message:'This Username is already taken',
                            url:'https://localhost:8080/RegistrationServlet.java',
                            data:'username'
                        },
                        type:'POST'


                }   

                },

and this is part of bootstrap username fields

<div class="form-group">
    <label class="col-xs-2 control-label">Username</label>

    <div class="col-xs-4">
        <input type="text" class="form-control" name="username" placeholder="Username" />
    </div></div>

嗨我有登记表格设计在引导我验证所有形式的领域使用远程验证器和我有registerdao.java方法具有checkusername布尔方法检查所有的行,如果存在则返回用户名用户名字符串否则假我checkusernameexists方法在registartiion servlet检查用户名当你输入用户名的领域。我不不明白为什么我的代码不显示任何东西。下面是代码不检查用户名和数据库和通知。如何编写JavaScript,核对数据的用户名或给我一些想法如何使此代码的工作吗?任何帮助将不胜appreaciated?谢谢

ReegisterDao.java public static boolean checkUserName(String userName) throws SQLException{ String checkUserNameQuery = "select * from EmployeeDetails where USER_NAME='"+userName+"'";

  try{


 // preparing some objects for connection
currentCon = ConnectionManager.getConnection();
Statement stmt = currentCon.createStatement();
rs  = stmt.executeQuery(checkUserNameQuery);
boolean more = rs.next();

    // if user does not exist set the isValid variable to false
    return more;
     }



catch (InstantiationException | IllegalAccessException e){}

return false; }

registrationservlet.java

public int checkUserNameExists(RegistrationBean user) throws SQLException{

        int a=RegistrationDao.Register(user);


    return a;

}

这是远程验证器验证用户名的一部分

   username: {
                    validators: {
                        notEmpty: {
                            message: 'The username is required'
                        },
                        stringLength: {
                            min: 6,
                            max: 30,
                            message: 'The username must be more than 6 and less than 30 characters long'
                        },
                        regexp: {
                            regexp: /^[a-zA-Z0-9_\.]+$/,
                            message: 'The username can only consist of alphabetical, number, dot and underscore'
                        },
                        remote:{
                            message:'This Username is already taken',
                            url:'https://localhost:8080/registrationservlet.java',
                            data:'username'
                        },
                        type:'POST'


                }   

                },

这是引导用户名字段的一部分

<div class="form-group">
    <label class="col-xs-2 control-label">Username</label>

    <div class="col-xs-4">
        <input type="text" class="form-control" name="username" placeholder="Username" />
    </div></div>
answer1: 回答1:

the return type Must be in valid json format . And I think whatever u r returning from servlet to that validation code is not in correct json form. use inspect element and in network tab see whats it is returning.

First you must change ur remote to like this

             remote: {
                        type: "POST",
                        url: 'RegistrationEmailCheck',//servlet
                        delay: 1000,

                        message: 'Your Message'
                    }

and from servlet return data like this

  public class RegistrationEmailCheck extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");

  PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
String availEmail = request.getParameter("email");
System.out.println(availEmail);
String SQL = "SELECT email FROM login WHERE email='" + availEmail + "'";
Connection con = DBInfo.getConn();
Statement st;
ResultSet rs;

try {
    st = con.createStatement();
    rs = st.executeQuery(SQL);


    if (rs.next()) {

        out.print("{\"valid\" : false }");
        json.put("valid", false);
        System.out.println("false");


    } else {

        out.print("{\"valid\" : true }");
        json.put("valid", true);
        System.out.println("true");


    }


} catch (Exception ex) {
    ex.printStackTrace();
} finally {

    out.close();
    }



}


}

This code is for email checking

the return type Must be in valid json format . And I think whatever u r returning from servlet to that validation code is not in correct json form. use inspect element and in network tab see whats it is returning.

首先你必须改变乌尔远程像这样

             remote: {
                        type: "POST",
                        url: 'RegistrationEmailCheck',//servlet
                        delay: 1000,

                        message: 'Your Message'
                    }

从Servlet返回这样的数据

  public class RegistrationEmailCheck extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");

  PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
String availEmail = request.getParameter("email");
System.out.println(availEmail);
String SQL = "SELECT email FROM login WHERE email='" + availEmail + "'";
Connection con = DBInfo.getConn();
Statement st;
ResultSet rs;

try {
    st = con.createStatement();
    rs = st.executeQuery(SQL);


    if (rs.next()) {

        out.print("{\"valid\" : false }");
        json.put("valid", false);
        System.out.println("false");


    } else {

        out.print("{\"valid\" : true }");
        json.put("valid", true);
        System.out.println("true");


    }


} catch (Exception ex) {
    ex.printStackTrace();
} finally {

    out.close();
    }



}


}

此代码用于电子邮件检查

jquery  username  instant