Ajax实现表单数据验证

阅读更多

一:实现表单数据验证:(这里使用的是responseText,"Post"方法发送请求)

客户端:index.jsp

首先是form表单:

 
昵称:

请输入密码:

请再次输入密码:

 js代码:

  

 服务器:servet代码:(省略conn.java和dao层)

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		UserDao dao = new UserDao();
		String name = request.getParameter("name");
		boolean flag = dao.selectUser(name);
		if(flag){
			out.print("1");
			System.out.print("测试成功");
		}else{
			dao.inserUser(name);
		}
	}

 其中用到了两个sql语句:

String sql = "insert into user (name) values ('"+name+"')";
String sql = "select * from user where name='"+name+"'";

 

 

 

你可能感兴趣的:(Ajax实现表单数据验证)