首先:login.jsp <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'index.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"> </head> <body> <form action="" method="post"> <input type="text" name="username" /> <input type="password" name="password" οnblur="return checkUser(this)"/> <input type="submit" values="提交" /> </form> </body> <mce:script type="text/javascript"><!-- var xmlHttpRequest; function createXmlHttpRequest(){ if(window.ActiveXObject){ //如果是IE浏览器 alert("ie"); return new ActiveXObject("Microsoft.XMLHTTP"); }else if(window.XMLHttpRequest){ //如果是非 IE浏览器 return new XMLHttpRequest(); } } function checkUser(password){ var apassword=password.value; if(!apassword){ alert("文本框不能为空"); //name.focus(); return; } //发送请求到服务器。判断用户名是否正确 //请求字符串 var url="LoginVal.do?password="+document.forms[0].password.value+"&username="+document.forms[0].username.value;; alert(url); //1.创建XMLHttpRequest组件 xmlHttpRequest=createXmlHttpRequest(); //2.初始化XMLHttpRequest组件 xmlHttpRequest.open("post",url,true); //3.设置回调参数 xmlHttpRequest.onreadystatechange=showAjax; //4.发送请求 xmlHttpRequest.send(null); } function showAjax(){ if(xmlHttpRequest.readyState==4&&xmlHttpRequest.status==200){ var b=xmlHttpRequest.responseText; if(b=="true"){ alert("密码正确OK!"); }else{ alert("密码错误!fail!"); } } } // --></mce:script> </html>