一、發送請求的html檔案
<html><head><title>ajax+jsp網域名稱查詢</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
var XMLHttpReq; //建立XMLHttpRequest對象 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");} catch (e) {}}}}//發送請求函數function sendRequest() { document.getElementById("comments").innerHTML ="正在查詢,請您稍等......";createXMLHttpRequest(); var name=document.getElementById("words").value; var url = "domainQuery1.jsp?words="+name;XMLHttpReq.open("GET", url, true);XMLHttpReq.onreadystatechange = processResponse;//指定響應函數XMLHttpReq.send(null); // 發送請求}// 處理返回資訊函數 function processResponse() { if (XMLHttpReq.readyState == 4) { // 判斷對象狀態 if (XMLHttpReq.status == 200) { // 資訊已經成功返回,開始處理資訊Display(); } else { //頁面不正常 window.alert("您所請求的頁面有異常。"); } } } function Display() { var msg=XMLHttpReq.responseText; document.getElementById(''comments'').value=msg; }</script>
</head>
<body bgColor=#cccca3>
請輸入欄位名名稱:<input type="text" value="cctv.com" id="words" name="words" size="69">
<input type="submit" value="查詢" id=Submit1 name=Submit1 onClick="sendRequest() ">
<TEXTAREA id="comments" name="comments" readonly rows=15 cols=76></TEXTAREA>
</body></html>
二、處理ajax請求的jsp檔案:
domainQuery1.jsp
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.net.HttpURLConnection
<