Ajax——異步檢查用戶名是否存在示例

字號:


    在任何網(wǎng)站注冊用戶的時候,都會檢查用戶是否已經(jīng)存在。很久以前的處理方式是將所有數(shù)據(jù)提交到服務(wù)器端進(jìn)行驗證,很顯然這種方式的用戶體驗很不好;后來有 了Ajax,有了異步交互,當(dāng)用戶輸完用戶名繼續(xù)填寫其他信息的時候,Ajax就將信息發(fā)到了服務(wù)器去檢查該用戶名是否已經(jīng)被注冊了,這樣如果用戶名已經(jīng) 存在,不用等用戶將所有數(shù)據(jù)都提交就可以給出提示。采用這種方式大大改善了用戶體驗。
    regist.jsp
    代碼如下:
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script type="text/javascript">
    var xmlHttp;
    //創(chuàng)建Ajax核心對象XMLHttpRequest
    function createXMLHttp(){
    if(window.XMLHttpRequest){
    xmlHttp = new XMLHttpRequest();
    }else{
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    }
    function checkUsername(username){
    createXMLHttp();
    //設(shè)置請求方式為GET,設(shè)置請求的URL,設(shè)置為異步提交
    xmlHttp.open("GET","CheckServlet?username="+username,true);
    //將方法地址復(fù)制給onreadystatechange屬性
    //類似于電話號碼
    xmlHttp.onreadystatechange = checkUsernameCallback();
    //將設(shè)置信息發(fā)送到Ajax引擎
    xmlHttp.send(null);
    }
    function checkUsernameCallback(){
    //Ajax引擎狀態(tài)為成功
    if(xmlHttp.readyState == 4){
    //HTTP協(xié)議狀態(tài)為成功
    if(xmlHttp.status == 200){
    var text = xmlHttp.responseText;
    if(text == "true"){
    document.getElementById("msg").innerHTML = "此用戶名已存在,無法使用!";
    }else{
    document.getElementById("msg").innerHTML = "此用戶名可以使用";
    }
    }
    }
    }
    </script>
    </head>
    <body>
    <form action="regist.jsp" method="post">
    用戶名:<input type="text" name="username" onblur="checkUsername(this.value)"><span id="msg"></span><br/>
    密 碼:<input type="password" name="password"><br/>
    <input type="submit" value="注冊">
    <input type="reset" value="重置">
    </form>
    </body>
    </html>
    CheckServlet.java
    代碼如下:
    public class CheckServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    public static final String DBURL = "jdbc:sqlserver://localhost:1433;DatabaseName=bbs";
    public static final String DBUSER = "sa";
    public static final String DBPASS = "pass";
    public CheckServlet() {
    super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html");
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    PrintWriter out = response.getWriter();
    String username = request.getParameter("usernaem");
    try{
    Class.forName(DBDRIVER);
    conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    String sql = "select count(username) from user where username=?";
    pst = conn.prepareStatement(sql);
    pst.setString(1,username);
    rs = pst.executeQuery();
    if(rs.next()){
    if(rs.getInt(1)>0){//用戶名已經(jīng)存在了
    out.print("true");
    }else{
    out.print("false");
    }
    }
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    try{
    conn.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }
    }