久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

java網(wǎng)絡(luò)編程-Ajax+servlet實(shí)例

 昵稱2807 2006-11-06
java網(wǎng)絡(luò)編程-Ajax+servlet實(shí)例

    Ajax異步請(qǐng)求,servlet產(chǎn)生隨機(jī)數(shù)據(jù),Html頁(yè)面無(wú)刷新顯示。
運(yùn)行環(huán)境:jdk1.5+tomcat 5.5

一、ajaxServlet.java

package com;
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 java.util.Random;
public class ajaxServlet extends HttpServlet{
private static final String CONTENT_TYPE="text/xml; charset=gb2312"; public void init() throws ServletException{} public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ response.setContentType(CONTENT_TYPE); response.setHeader("Cache-Control","no-cache"); response.setHeader("Pragma","no-cache"); response.setDateHeader("Expires",0); PrintWriter out=response.getWriter(); String action=request.getParameter("action"); if(("send").equals(action)){ Random random = new Random();  String AA=Integer.toString(Math.abs(random.nextInt())); String BB=Integer.toString(Math.abs(random.nextInt()));  String CC=Integer.toString(Math.abs(random.nextInt())); String DD=Integer.toString(Math.abs(random.nextInt())); StringBuffer sb=new StringBuffer(); sb.append(AA); sb.append("@@@"); sb.append(BB); sb.append("@@@"); sb.append(CC); sb.append("@@@"); sb.append(DD); out.write(sb.toString()); out.close(); } }}

二,、testajax.html
<html>
<head>
<title>AJAX.html</title>
</head>
<script type="text/javascript">

function getResult(){
var url = "/Tajax/AajaxServlet?action=send";
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}else if (window.ActiveXObject){
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(req){
req.open("GET",url, true);
req.onreadystatechange = complete;
req.send(null);
}
}
/*分析返回的文本文檔*/
function complete(){
if (req.readyState == 4) {
if (req.status == 200) {
// alert(req.responseText);
var strResult = unescape(req.responseText);
var arrResult = strResult.split("@@@");
RemoveRow();  //刪除以前的數(shù)據(jù).
num1 = arrResult[0]; //字段num1的值
num2 = arrResult[1]; //字段num2的值
num3 = arrResult[2];
num4 = arrResult[3];
row1= tb.insertRow();
cell1 = row1.insertCell();
cell1.innerText = num1;
cell2 = row1.insertCell();
cell2.innerText = num2;
row2= tb.insertRow();
cell3 = row2.insertCell();
cell3.innerText = num3;
cell4 = row2.insertCell();
cell4.innerText = num4;
} else {
alert(‘There was a problem with the request.‘);
}
}
}
function RemoveRow(){//保留第一行表頭,其余數(shù)據(jù)均刪除.
var iRows = tb.rows.length;
for(var i=0;i< iRows-1;i++){
tb.deleteRow(1);
}
}
function MyShow(){//2秒自動(dòng)刷新一次,2秒取得一次數(shù)據(jù).
timer = window.setInterval("getResult()",2000);
}

</script>

 

<body onload="MyShow()">
<table width="47%" height="23" border="0" cellpadding="1" cellspacing="0" id="tb">
<tr><td>代碼</td>
<td>價(jià)格</td>
</tr>
</table>

<form>
<p>輸入股票代碼:
<input type="text" size="14" name="phone" id="phone"/>
</p>
<p>你交易的數(shù)量:</p>
<p><textarea name="order" rows="6" cols="50" id="order"></textarea></p>
<p><input type="submit" value="提交" id="submit" /></p>
</form>

</body>


  三、web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java./xml/ns/j2ee"
xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://java./xml/ns/j2ee http://java./xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>

<servlet>
<servlet-name>AajaxServlet</servlet-name>
<servlet-class>com.ajaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AajaxServlet</servlet-name>
<url-pattern>/AajaxServlet</url-pattern>
</servlet-mapping>


</web-app>

運(yùn)行圖:

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多