嗨,我在bootstrap中设计了注册表单,我使用远程验证器验证所有表单字段,我有registerdao.java方法,其中有checkusername布尔方法,检查所有行,如果用户名存在,它返回用户名字符串否则为false,我有checkusernameexists方法在注册servlet中,当您输入用户名字段时检查用户名。我不明白为什么我的代码没有显示任何东西。下面是代码不检查用户名对数据库和通知。如何编写javascript,使其检查用户名对数据或给我一些想法,如何使这段代码的工作?任何帮助都将非常感激。由于
ReegisterDao.javacheckUserName(String userName)抛出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){}
返回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'
}
},
这是bootstrap用户名字段
的一部分<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>
返回类型必须是合法的json格式。我认为,无论你从servlet返回到验证代码,都不是正确的json格式。使用inspect元素并在network TAB中查看它返回的结果。
首先你必须改变你的遥控器像这样
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();
}
}
}
此代码用于检查电子邮件