这次是验证用户名的唯一性。因为篇幅有限!所以没有domain层等! 数据库自己创建就可以了,包含uname和uid
jsp页面
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp1.jsp' starting page</title>
</head>
<body>
<form action="MyJsp.jsp" method="post">
用户姓名<input type="text" id="uname" οnblur="checkUserid(this.value)"/><span id="msg"></span><br/>
用户id:<input type="text" id="uid"/>
<br/>
<input type="submit" value="提交" />
</form>
</body>
</html>
<script type="text/javascript">
var xmlHttp;
function createXMLHttp(){
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();//支持火狐浏览器
}else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//支持IE浏览器
}
}
function checkUserid(uname){
createXMLHttp();
xmlHttp.open("POST","./servlet/checkServlet",true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.onreadystatechange=checkUseridcallBack;
xmlHttp.send("uname="+uname);
}
function checkUseridcallBack(){
if(xmlHttp.readyState == 4){//验证请求应答是否完成
if(xmlHttp.status == 200){//验证是否正确
var text=xmlHttp.responseText;
if(text=="true"){
document.getElementById("msg").innerHTML="该用户已存在";
}else{
document.getElementById("msg").innerHTML="您可使用此ID";
}
}
}
}
</script>
servlet页面
package com.csdn.day03.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class checkServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/mldn";
public static final String DBUSER="root";
public static final String DBPASS="1234";
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("utf-8");
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
String uname=request.getParameter("uname");
PrintWriter out=response.getWriter();
try{
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql = "select COUNT(uname) from user where uname=?";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,uname);
rs=pstmt.executeQuery();
if(rs.next()){
if(rs.getInt(1)>0){
out.print("true");
}else{
out.print("false");
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}