关于一个小小的ajax用于java web的问题
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
<head>
<title>表单验证</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
function formcheck(){
var xmlHttpRequest;
if(window.XMLHttpRequest){
xmlHttpRequest = new XMLHttpRequest();
}else{
xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpRequest.onreadystatechange=showresult;
var url="FormCheck";
var params="userid="+userid.value+"&userpwd="+userpwd.value;
xmlHttpRequest.open("POST",url, true);
xmlHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlHttpRequest.send(params);
}
function showresult(){
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
var info = xmlHttpRequest.responseText;
result.innerHTML=info;
}
}
}
</script>
</head>
<body>
请输入用户名:<input type="text" name="userid" onblur="formcheck()"/><br/>
请输入密码 :<input type="password" name="userpwd"/><br/>
<input type="button" value="登录" onclick="formcheck()"/>
<div id="result"></div>
</body>
</html>
import
import
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//省略了导入包
public class FormCheck extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("utf-8");
String userid=request.getParameter("userid");
if(!"张三".equals(userid)){
String mgs1="用户名不存在";
out.print(mgs1);
System.out.println(mgs1);
}
else{
String userpwd=request.getParameter("userpwd");
if(!"".equals(userpwd)){
if("123".equals(userpwd))
{
out.print("欢迎您");
System.out.println("欢迎您");
}
else
{
out.print("密码错误");
System.out.println("密码错误");
}
}
}
}
}