用ajax实现用户名的检测(JavaScript方法)

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <script type="text/javascript">
10 var req;
11 function check()
12 {
13     var url ="CheckUser?uname="+ document.getElementById("uname").value;
14     if (window.XMLHttpRequest)
15     {
16         req = new XMLHttpRequest();
17     }
18     else if (window.ActiveXObject){
19         req = new ActiveXObject("Microsoft.XMLHTTP");
20     }       
21     req.open("GET",url, true);
22     req.onreadystatechange = show;
23     req.send(null);
24     alert("hhhh");
25 }
26 function show(){
27     if (req.readyState == 4) {
28         if (req.status == 200) {
29             document.getElementById("info").innerHTML=req.responseText;
30         }
31     }
32 }
33 </script>
34 <body>
35 <table>
36 <tr><td>用户名</td><td><input type="text" id="uname" ></td></tr>
37 <tr><td><input type="button" onClick="check()" value="检测用户"></td><td><div id="info"></div> </td></tr>
38 </table>
39 </body>
40 </html>
 1 package javastudy;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.annotation.WebServlet;
 6 import javax.servlet.http.HttpServlet;
 7 import javax.servlet.http.HttpServletRequest;
 8 import javax.servlet.http.HttpServletResponse;
 9 
10 /**
11  * Servlet implementation class CheckUser
12  */
13 @WebServlet("/CheckUser")
14 public class CheckUser extends HttpServlet {
15     private static final long serialVersionUID = 1L;
16        
17     /**
18      * @see HttpServlet#HttpServlet()
19      */
20     public CheckUser() {
21         super();
22         // TODO Auto-generated constructor stub
23     }
24 
25     /**
26      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
27      */
28     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
29         // TODO Auto-generated method stub
30         String uname=request.getParameter("uname");
31         String[] names={"zhang","yang","wang"};
32         boolean flag=true;
33         for(int i =0;i<names.length;i++)
34         {
35             if(names[i].equals(uname))
36             {
37                 flag=false;
38                 break;
39             }
40             
41         }
42         response.setCharacterEncoding("utf-8");
43         if(flag==false)
44         {
45             response.getWriter().println("该用户名已注册!!!");
46         }
47         else
48         {
49             response.getWriter().println("注册成功!!!");
50         }
51 //    response.getWriter().append("Served at: ").append(request.getContextPath());
52     }
53 
54     /**
55      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
56      */
57     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
58         // TODO Auto-generated method stub
59     doGet(request, response);
60     }
61 
62 }

 

posted @ 2016-01-02 12:44  夏冬青  阅读(420)  评论(0编辑  收藏  举报