AJAX

来源:互联网 发布:ea自动交易软件 编辑:程序博客网 时间:2024/05/18 08:24

AJAX 的小应用,小范围刷新,判断用户名是否已经注册。

我最后才发现传值的时候如果是中文就会出现乱码问题,所有我用了网上教的两次编码1次解码去解决问题了。

1、页面代码

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP '14-4-21_AJAS_logon.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   
  24.   </head>  
  25.   <script type="text/javascript">  
  26.   var xhr=null;  
  27.     
  28.   function checkUname(){  
  29.       var username=document.myform.uname.value;  
  30.       if(username==""||username==null){  
  31.             document.getElementById("msgDiv").innerHTML="<font color='red'>请输入用户名</font>";  
  32.           }  
  33.           
  34.         if(window.ActiveXObject){  
  35.             xhr=new ActiveXObject("Microsoft.XMLHTTP");  
  36.             }else{  
  37.                 xhr=new XMLHttpRequest();//只对非IE浏览器生效  
  38.                   
  39.                 }  
  40.       
  41.         var url="http://localhost:8080/Five_0001_Kt/TestServlet_005_14421_Login?uname="+username;  
  42.         url=encodeURI(url);  
  43.         url=encodeURI(url); //两次编码  
  44.         xhr.onreadystatechange=haolejiaowo;   //当核心对象状态发生改变,它的处理函数  
  45.         xhr.open("post",url,true);  
  46.         xhr.send(null);  
  47.           
  48.       }  
  49.   function haolejiaowo(){  
  50.       if(xhr.readyState==4&&xhr.status==200){  
  51.         var result=xhr.responseText;//得到远程服务器返回的结果  
  52.         if(result.indexOf("true")!=-1){  
  53.           document.getElementById("msgDiv").innerHTML="<font color='red'>用户名已存在</font>";;  
  54.         }else{  
  55.           document.getElementById("msgDiv").innerHTML="可以注册";  
  56.         }  
  57.         }  
  58.       }  
  59.   
  60.     
  61.   </script>  
  62.     
  63.     
  64.   <body>  
  65.     <form action="#" method="post" name="myform" >  
  66.     
  67.     <input type="text" name="uname" onblur="checkUname()"/>  
  68.     <div id="msgDiv" style="display:inline">aaa</div>  
  69.     <br/>  
  70.     <input type="submit"value="注册">   
  71.     </form>  
  72.       
  73.       
  74.       
  75.   </body>  
  76. </html>  

2、servlet代码

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package PractiseServlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. public class TestServlet_005_14421_Login extends HttpServlet {  
  12.   
  13.     public boolean isExist(String name){  
  14.         boolean flag=false;  
  15.         String []names={"admin","xiaopang","房智晨"};  
  16.         for(String s:names){  
  17.             if(name.equals(s)) flag=true;  
  18.         }  
  19.         return flag;  
  20.     }  
  21.       
  22.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  23.             throws ServletException, IOException {  
  24.         this.doPost(request, response);  
  25.     }  
  26.   
  27.   
  28.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  29.             throws ServletException, IOException {  
  30.         response.setContentType("text/html;charset=utf-8");  
  31.           
  32.         PrintWriter out = response.getWriter();  
  33.       
  34.         String username=request.getParameter("uname");  
  35.         username = java.net.URLDecoder.decode(username, "UTF-8");//一次解码  
  36.         System.out.println(username);  
  37.         boolean flag=isExist(username);  
  38.         out.println(flag);  
  39.           
  40.         out.flush();  
  41.         out.close();      
  42.           
  43.     }  
  44.   
  45. }  


3、页面效果


0 0
原创粉丝点击