<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <script type="text/javascript" src="js/test.js"></script><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> <form > 使用者名稱:<input type="text" id="name" /><br/> <input type="submit" id="OK" value="校正" /> </form> </body></html>function ajaxFunction() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("您的瀏覽器不支援AJAX!"); return false; } } } return xmlHttp; }window.onload=function(){document.getElementById("OK").onclick=function(){//alert("wuuuu");/* * 1.擷取xmlHttpRequest * */var xmlReq=ajaxFunction();/* * 4.接手伺服器的響應 * * *///alert("wuuuu");xmlReq.onreadystatechange=function(){if(xmlReq.readyState==4){alert(xmlReq.readyState);alert(xmlReq.status);if(xmlReq.status==200||xmlReq.status==304){alert("wu");var data=xmlReq.responseText;alert(data);}}}/* * 2.開啟伺服器的串連, * 第三個參數為true(非同步) * 第一個參數:傳遞方法 * 第二個方法是路徑 * *///alert("路徑整的不對啊???怎麼改過來???");xmlReq.open("get","/Ajax/Demo1",true);alert("adsada");/* * 3.發送資料 * */xmlReq.send(null);}}package cn.com.Demo;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.jsp.PageContext;public class Demo1 extends HttpServlet {/** * */private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {request.setCharacterEncoding("UTF-8");response.setContentType("text/html; charset=utf-8");PrintWriter out=response.getWriter();//out.write("OK");System.out.println("--------------------------------------");}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request,response);}}