Ajax 註冊非同步驗證小例子

來源:互聯網
上載者:User

 

 

資料庫表結構:

 

表中有一條記錄:

 

項目結構:

 

register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%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 'register.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>  <script type="text/javascript">  var xmlHttp;  var flag;  function createXMLHttp()  {  if(window.XMLHttpRequest)  {  xmlHttp = new XMLHttpRequest();  }  else  {  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  }  }  function checkUserId(userid)  {  createXMLHttp();  xmlHttp.open("POST", "CheckServlet?userid="+userid);  xmlHttp.onreadystatechange = checkUserIdCallback;  xmlHttp.send(null);  document.getElementById("msg").innerHTML = "正在發送...";    }  function checkUserIdCallback()  {  if(xmlHttp.readyState == 4)  {  if(xmlHttp.status == 200)  {  var text = xmlHttp.responseText;  if(text == "true"){  flag = false;  document.getElementById("msg").innerHTML = "使用者ID重複,無法使用!";  }  else  {  flag = true;  document.getElementById("msg").innerHTML = "此使用者ID可以註冊!";  }  }  }  }  function checkForm()  {  return flag;  }  </script>    <body>    <form action="jsp/register.jsp" method="post" onsubmit="checkForm()">    使用者ID:<input type="text" name="userid" onblur="checkUserId(this.value)"><span id="msg"></span><br>    姓  名:<input type="text" name="name"><br>    密  碼:<input type="password" name="password"><br>    <input type="submit" value="註冊">    <input type="reset" value="重設">    </form>  </body></html>

CheckServlet.java

package com.xu.servlets;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 {public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";public static final String DBURL = "jdbc:mysql://localhost:3306/mydb";public static final String DBUSER = "root";public static final String DBPASS = "root";private static final long serialVersionUID = 7502494116656742257L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("utf-8");response.setContentType("text/html");Connection conn = null;PreparedStatement pstmt = null;ResultSet rs = null;PrintWriter out = response.getWriter();String userid = request.getParameter("userid");try{Class.forName(DBDRIVER);conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);String sql = "SELECT COUNT(userid) FROM user WHERE userid = ?";pstmt = conn.prepareStatement(sql);pstmt.setString(1, userid);rs = pstmt.executeQuery();if(rs.next()){if(rs.getInt(1)>0){out.print("true");}else{out.print("false");}}out.close();}catch (Exception e) {e.printStackTrace();}finally{try{conn.close();}catch (Exception e) {e.printStackTrace();}}}}

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  <display-name></display-name>  <servlet>    <description>This is the description of my J2EE component</description>    <display-name>This is the display name of my J2EE component</display-name>    <servlet-name>CheckServlet</servlet-name>    <servlet-class>com.xu.servlets.CheckServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>CheckServlet</servlet-name>    <url-pattern>/CheckServlet</url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>

運行結果:

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.