]ajax和jsp组合进行域名查询[转]

一、发送请求的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" %>
<%@ page import="java.net.URL" %>
<%@ page import="org.jdom.Document" %>
<%@ page import="org.jdom.Element" %>
<%@ page import="org.jdom.input.*" %>

<%@ page contentType="text/html; charset=gb2312" %>
<%


  //设置输出信息的格式及字符集       
        response.setContentType("text/html; charset=gb2312");
        response.setHeader("Cache-Control","no-cache");
        String param =request.getParameter("words");
        String domainmsg=null;
        URL url = null;
        BufferedReader in = null;
        InputStreamReader isr = null;
        InputStream is = null;
        HttpURLConnection huc = null;
        try
        {
            url = new URL("http://now.net.cn/domain/domaincheck.php?query="+param);
            huc = (HttpURLConnection)url.openConnection();
         
            is = huc.getInputStream();
            isr = new InputStreamReader(is);
            in = new BufferedReader(isr);
            String line = null;
            StringBuffer s = new StringBuffer(333);
            while((line = in.readLine()) != null) {
                line=line.trim();
                s.append(line);
            }
            domainmsg=s.toString();
            SAXBuilder builder = new SAXBuilder();
            Document doc = null;
            Reader in1= new StringReader(domainmsg);
            doc = builder.build(in1);
            Element root = doc.getRootElement();
          
            String ss=root.getChild("result").getChildText("msg");
            out.print(ss);
        }catch(Exception e){
           e.printStackTrace();
        }
        finally
        {
            try
            {
                huc.disconnect();
                is.close();
                isr.close();
                in.close();
            }
            catch(Exception e)
            {
               e.printStackTrace();
            }
        }
        out.close();
%>

你可能感兴趣的:(jsp,Ajax,.net,浏览器,IE)