<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">



<html>


<head>


<title>My JSP 'ajaxTest.jsp' starting page</title>


<script type="text/javascript" src="ajaxTest.js"></script>


</head>





<body>


enter :


<input type="text" name="sth" id="sth" />


<input type="button" value="查看输入的内容" οnclick="myRequest()"/>


<br />


<label id="out"></label>


</body>






</html>






ajaxTest.js






var http_request = false;



function sendRequest(method, url, content, responseType, callback) {


http_request = false;


if (window.XMLHttpRequest) {


http_request = new XMLHttpRequest();


if (http_request.overrideMimeType) {


http_request.overrideMimeType("text/xml");


}


} else {


if (window.ActiveXObject) {


try {


http_request = new ActiveXObject("Msxml2.XMLHTTP");


}


catch (e) {


try {


http_request = new ActiveXObject("Microsoft.XMLHTTP");


}


catch (e) {


}


}


}


}


if (!http_request) {


window.alert("\不\能\创\建XMLHttpRequest\对\象\实\例");


return false;


}


if (responseType.toLowerCase() == "text") {


http_request.onreadystatechange = callback;


} else {


if (responseType.toLowerCase() == "xml") {


http_request.onreadystatechange = callback;


} else {


window.alert("\相\应\参\数\类\别\错\误");


return false;


}


}


if (method.toLowerCase() == "get") {


http_request.open(method, url, true);


} else {


if (method.toLowerCase() == "post") {


http_request.open(method, url, true);


http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


} else {


window.alert("http\请\求\类\别\参\数\错\误");


return false;


}


}


http_request.send(content);



}



function processResponse() {


if (http_request.readyState == 4) {


if (http_request.status == 200) {


var msg = document.getElementById("out");


if (msg != undefined) {


msg.innerText = http_request.responseText;


}


} else {


alert("\您\所\请\求\的\页\面\有\异\常");


}


}



}






function myRequest(path) {


var name;


if (document.getElementById("sth")!= undefined) {


name = document.getElementById("sth").value;


}


var url = "servlet/AjaxTest?time=" + Math.random();


var content = "sth=" + name;


sendRequest("POST", url, content, "TEXT", processResponse);



}






servlet






package com.bonree.servlet;






import java.io.IOException;






import javax.servlet.ServletException;



import javax.servlet.http.HttpServlet;



import javax.servlet.http.HttpServletRequest;



import javax.servlet.http.HttpServletResponse;






public class AjaxTest extends HttpServlet {





public void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {





this.doPost(request, response);


}





public void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {


response.setContentType("text/html");


response.setCharacterEncoding("utf-8");


request.setCharacterEncoding("utf-8");


String name = request.getParameter("sth");


response.getWriter().write("输入的内容为:" + name);





}






}