login.jsp页面
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8">
</head>
<script type="text/javascript">
var XMLHttpReq=false;
function createXMLHttpRequest(){
if(window.XMLHttpRequest){ //Mozilla浏览器
XMLHttpReq=new XMLHttpRequest();
}else if(window.ActiveXObject){ //IE
try{
XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
alert("5");
}catch(e){
alert("6");
}
}
}
}
function sendRequest(url){
createXMLHttpRequest();
XMLHttpReq.open("GET",url,true);
XMLHttpReq.onreadystatechange=processResponse; //指定响应函数
XMLHttpReq.send(null);
}
function processResponse(){
if(XMLHttpReq.readyState==4){
if(XMLHttpReq.status==200){
var res=XMLHttpReq.responseXML.getElementsByTagName("res")[0].text;
window.alert(res);
}else{
window.alert("您所请求的页面有异常");
}
}
}
//身份的验证函数,验证通过提交对应的请求
function userCheck(){
var uname=document.one.name.value;
var psw=document.one.pass.value;
if(uname==""){
window.alert("用户名不能为空");
document.one.name.focus();
return false;
}else{
sendRequest('/firstAjax/Login?name='+uname+'&pass='+psw);
}
}
</script>
<body><br> <form method="get" name="one"><p> </p><p> </p><p> <br></p><table width="300" border="1" height="111">
<tbody><tr>
<td> <input type="text" name="name"></td>
<td valign="top"><br></td><td> 名字</td></tr>
<tr>
<td> <input type="password" name="pass"></td>
<td valign="top"><br></td><td> 密码</td></tr>
</tbody></table><p> </p><p> <input type="button" onclick="userCheck()" value="提交" name="btnaction"></p><p> </p></form></body>
</html>
请求的SEVLET页面protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
String name=req.getParameter("name");
String pass=req.getParameter("pass");
PrintWriter out=res.getWriter();
System.out.println("hello");
res.setContentType("text/xml;charset=UTF-8");
res.setHeader("Cache-Control", "no-cache");
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<response>");
if(name.equals("gao")&&pass.equals("123")){
out.println("<res>"+"\u5f53\u5f53"+"</res>");
}else{
out.println("<res>"+"\u5f53\u5f53"+"</res>");
}
out.println("</response>");
out.flush();
out.close();
}
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8">
</head>
<script type="text/javascript">
var XMLHttpReq=false;
function createXMLHttpRequest(){
if(window.XMLHttpRequest){ //Mozilla浏览器
XMLHttpReq=new XMLHttpRequest();
}else if(window.ActiveXObject){ //IE
try{
XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
alert("5");
}catch(e){
alert("6");
}
}
}
}
function sendRequest(url){
createXMLHttpRequest();
XMLHttpReq.open("GET",url,true);
XMLHttpReq.onreadystatechange=processResponse; //指定响应函数
XMLHttpReq.send(null);
}
function processResponse(){
if(XMLHttpReq.readyState==4){
if(XMLHttpReq.status==200){
var res=XMLHttpReq.responseXML.getElementsByTagName("res")[0].text;
window.alert(res);
}else{
window.alert("您所请求的页面有异常");
}
}
}
//身份的验证函数,验证通过提交对应的请求
function userCheck(){
var uname=document.one.name.value;
var psw=document.one.pass.value;
if(uname==""){
window.alert("用户名不能为空");
document.one.name.focus();
return false;
}else{
sendRequest('/firstAjax/Login?name='+uname+'&pass='+psw);
}
}
</script>
<body><br> <form method="get" name="one"><p> </p><p> </p><p> <br></p><table width="300" border="1" height="111">
<tbody><tr>
<td> <input type="text" name="name"></td>
<td valign="top"><br></td><td> 名字</td></tr>
<tr>
<td> <input type="password" name="pass"></td>
<td valign="top"><br></td><td> 密码</td></tr>
</tbody></table><p> </p><p> <input type="button" onclick="userCheck()" value="提交" name="btnaction"></p><p> </p></form></body>
</html>
请求的SEVLET页面protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("UTF-8");
String name=req.getParameter("name");
String pass=req.getParameter("pass");
PrintWriter out=res.getWriter();
System.out.println("hello");
res.setContentType("text/xml;charset=UTF-8");
res.setHeader("Cache-Control", "no-cache");
out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println("<response>");
if(name.equals("gao")&&pass.equals("123")){
out.println("<res>"+"\u5f53\u5f53"+"</res>");
}else{
out.println("<res>"+"\u5f53\u5f53"+"</res>");
}
out.println("</response>");
out.flush();
out.close();
}
var res=XMLHttpReq.responseXML.getElementsByTagName("res")[0].text;
window.alert(res); 弹出的地方乱码
ajaxpost.js
var myRequest; // Variable to hold request object
function mysubmit()...{
if (window.XMLHttpRequest)
...{
myRequest = new XMLHttpRequest(); // Standards-compliant browsers
} else if (window.ActiveXObject)
...{
myRequest = new ActiveXObject("Msxml2.XMLHTTP"); // For IE
} var post="name="+document.getElementById("postval").value;
post=encodeURI(post);
post=encodeURI(post); //最重要的部分,两次调用encodeURI ,就是编码两次
myRequest.open("POST","servlet/Display",false);
//myRequest.setRequestHeader("contentLength",post.length);
myRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
myRequest.send(post);
var res=myRequest.responseText;//接收返回的数据
document.getElementById("display").innerHTML=res;
}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhanngle/archive/2008/01/24/2063490.aspx
servlet
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{ response.setContentType("text/html");
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String name=request.getParameter("name");
name=URLDecoder.decode(name,"utf8"); //post 传递的时候,一定是用utf8编码的,url 自己可以设定
System.out.println(name);
out.println(name);
out.flush();
out.close();
}本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhanngle/archive/2008/01/24/2063490.aspx