AJAX+JSP GET与POST方式参数传递的应用

参考链接:http://shenymce.blog.51cto.com/337979/693868

 

Ajax.html页面

  1. <!DOCTYPE html> 
  2. <html> 
  3.     <head> 
  4.         <title></title> 
  5.         <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
  6.         <script type="text/javascript"> 
  7.             var xmlhttp;  
  8.             function SubMit()  
  9.             {  
  10.                 //非IE浏览器XMLHttpRequest对象的创建  
  11.                 if (window.XMLHttpRequest)  
  12.                     {  
  13.                         xmlhttp=new XMLHttpRequest();  
  14.                     }  
  15.                     //IE浏览器XMLHttpRequest对象的创建  
  16.                     else if(window.ActiveXObject)  
  17.                     {  
  18.                         var activename=["Msxml2.XMLHTTP","Msxml.XMLHTTP","Microsoft.XMLHTTP"];  
  19.                         for (i=0;activename.length;i++)  
  20.                             {  
  21.                                 try{  
  22.                                     xmlhttp=new ActiveXObject(activename[i]);  
  23.                                     break;  
  24.                                 }  
  25.                                 catch(e){}  
  26.                             }              
  27.                     }  
  28.                if (xmlhttp)  
  29.                   {   
  30.                        document.getElementById("message").innerHTML="开始用户名测试...";  
  31.                        window.setTimeout('postxml()',2000);  //2秒后执行          
  32.                    }  
  33.                    else  
  34.                    {  
  35.                      alert("你的浏览器不支持XMLHttpRequest对象");        
  36.                    }  
  37.  
  38.  
  39.             }  
  40.             function godo()  
  41.             {  
  42.                   //判断服务器是否有返回值  
  43.                    if(xmlhttp.readyState == 4)  
  44.                    //判断HTTP请求是否正确  
  45.                        {  
  46.                            if(xmlhttp.status  == 200)  
  47.                            {  
  48.                                //获得服务器返回的数据  
  49.                                document.getElementById("message").innerHTML=xmlhttp.responseText;  
  50.                            }  
  51.                        }  
  52.             }  
  53.             //采用get方式传递参数  
  54.             function getxml()  
  55.             {  
  56.                var uname=document.getElementById("UserName");  
  57.                xmlhttp.open("get","AjaxServlet?uname="+uname.value,true)  
  58.                xmlhttp.onreadystatechange=godo;  
  59.                xmlhttp.send(null);      
  60.             }  
  61.             //采用post方式传递参数  
  62.              function postxml()  
  63.             {  
  64.                var uname=document.getElementById("UserName");  
  65.                xmlhttp.open("post","AjaxServlet",true)  
  66.                xmlhttp.onreadystatechange=godo;  
  67.                xmlhttp.setRequestHeader ("Content-Type","application/x-www-form-urlencoded");  
  68.                xmlhttp.send("uname="+uname.value);      
  69.             }             
  70.         </script> 
  71.     </head> 
  72.     <body> 
  73.         <input type="text" id="UserName"/> 
  74.         <input type="button" value="用户名验证" onclick="SubMit();"/> 
  75.         <div id="message"></div> 
  76.     </body> 
  77. </html> 

AjaxServlet.java页面

  1. protected void processRequest(HttpServletRequest request, HttpServletResponse response)  
  2.          throws ServletException, IOException {  
  3.      response.setContentType("text/html;charset=UTF-8");  
  4.      PrintWriter out = response.getWriter();  
  5.      try {  
  6.          String old=request.getParameter("uname");  
  7.          if(old == null || "".equals(old))  
  8.          {  
  9.              out.println("用户名不可以为空");  
  10.          }else{  
  11.              String name = new String(old.getBytes("ISO8859-1"),"gb2312");  
  12.              System.out.println(name);  
  13.              if(name.equals("123"))  
  14.              {  
  15.                  out.println("用户名"+ name + "已经存在!");  
  16.                    
  17.              }else{  
  18.                  out.println("用户名"+ name + "可以注册!");  
  19.              }  
  20.          }  
  21.      } finally {              
  22.          out.close();  
  23.      }  
  24.  }

你可能感兴趣的:(Ajax)