jsp頁面: Js代碼
- <script type="text/javascript" language="javascript">
- var xmlHttp = false;
- try {
- xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
- } catch (e) {
- try {
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- } catch (e2) {
- xmlHttp = false;
- }
- }
- if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
- xmlHttp = new XMLHttpRequest();
- }
-
- //檢測使用者名稱是否存在
- function callServerCheckUname() {
- //通過GET方法請求,將請求封裝在URL中
- var uname = document.getElementById("u_name").value;
- xmlHttp.open("POST", 'CheckUname?u_name='+uname);
- //設定伺服器響應的處理方法
- xmlHttp.onreadystatechange = updatePageCheckUname;
- // 發送請求
- xmlHttp.send(null);
- }
- function updatePageCheckUname() {
- if (xmlHttp.readyState == 4) {
- var response = xmlHttp.responseText;
- if(response >= 1){
- alert("使用者名稱已存在!");
- }else{
- alert("可註冊!");
- }
- }
- }
- </script>
<script type="text/javascript" language="javascript">var xmlHttp = false;try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { xmlHttp = false; }}if (!xmlHttp && typeof XMLHttpRequest != 'undefined') { xmlHttp = new XMLHttpRequest();}//檢測使用者名稱是否存在function callServerCheckUname() { //通過GET方法請求,將請求封裝在URL中 var uname = document.getElementById("u_name").value; xmlHttp.open("POST", 'CheckUname?u_name='+uname); //設定伺服器響應的處理方法 xmlHttp.onreadystatechange = updatePageCheckUname; // 發送請求 xmlHttp.send(null);}function updatePageCheckUname() { if (xmlHttp.readyState == 4) { var response = xmlHttp.responseText; if(response >= 1){ alert("使用者名稱已存在!"); }else{ alert("可註冊!"); } }}</script>
web.xml設定檔: Xml代碼
- <servlet>
- <servlet-name>CheckUname</servlet-name>
- <servlet-class>servlet.AjaxCheckUname</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>CheckUname</servlet-name>
- <url-pattern>/CheckUname</url-pattern>
- </servlet-mapping>
<servlet> <servlet-name>CheckUname</servlet-name> <servlet-class>servlet.AjaxCheckUname</servlet-class> </servlet><servlet-mapping> <servlet-name>CheckUname</servlet-name> <url-pattern>/CheckUname</url-pattern> </servlet-mapping>
servlet頁面: Java代碼
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=GB2312");
- PrintWriter out = response.getWriter();
- try {
- String uname = request.getParameter("u_name");
- RegistDAOImpl regDao = new RegistDAOImpl();
- out.print(regDao.checkUname(uname)) ;
- } catch (Exception e) {
- }
- out.flush();
- out.close();
- }
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=GB2312");PrintWriter out = response.getWriter();try {String uname = request.getParameter("u_name");RegistDAOImpl regDao = new RegistDAOImpl();out.print(regDao.checkUname(uname)) ;} catch (Exception e) {}out.flush();out.close();}
DAO實作類別: Java代碼
- public int checkUname(String uname){
- Connection conn = null;
- PreparedStatement pstm = null;
- ResultSet rs = null;
- int success = 0;
- String sql = "select u_name from u_info where u_name='"+uname+"' or u_mobtel='"+uname+"' or u_email='"+uname+"'";
- try {
- conn = getConnection();
- pstm = conn.prepareStatement(sql);
- rs = pstm.executeQuery(sql);
- if(rs.next()){
- success = 1;
- }
- return success;
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- conn.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return success;
- }