大家好我在bootstrap中设计了注册表单,我使用远程验证器验证了所有表单文件,我有registerdao.java方法,它有checkusername布尔方法检查所有行,如果用户名存在,则返回用户名字符串,否则为false我在registartiion servlet中有checkusernameexists方法,当你输入用户名字段时检查用户名。我不明白为什么我的代码没有显示任何东西。下面是代码没有检查用户名对数据库和通知。如何编写javascript以便它检查用户名对数据或给我一些想法如何使这段代码工作?任何帮助都会有很大的帮助吗?谢谢
ReegisterDao.java public static boolean checkUserName(String userName)throws SQLException { String checkUserNameQuery =“select * from EmployeeDetails 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>
答案 0 :(得分:0)
返回类型必须是有效的json格式。我认为从servlet返回到验证代码的任何东西都不是正确的json形式。 使用inspect元素并在网络标签中查看它正在返回的内容。
首先你必须将你的遥控器更改为喜欢这个
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();
}
}
}
此代码用于检查电子邮件