java网络编程-Ajax servlet实例

文章作者 100test 发表时间 2007:03:14 16:27:29
来源 100Test.Com百考试题网


Ajax异步请求,servlet产生随机数据,Html页面无刷新显示。
运行环境: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


AJAX.html

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().  //删除以前的数据.

      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(){//保留第一行表头,其余数据均删除.

    var iRows = tb.rows.length.

    for(var i=0.i< iRows-1.i  ){

       tb.0deleteRow(1).

    }

 }

 function MyShow(){//2秒自动刷新一次,2秒取得一次数据.

  timer = window.setInterval("getResult()",2000).

  }


onload="MyShow()">
width="47%" height="23" border="0" cellpadding="1" cellspacing="0" id="tb">
代码
价格


输入股票代码:


你交易的数量:





  三、web.xml

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

Welcome to Tomcat

Welcome to Tomcat


AajaxServlet
com.ajaxServlet


AajaxServlet
/AajaxServlet



运行图:



相关文章


ajax框架,webui库--qooxdoo使用感想
用java.nio.*进行网络编程
java网络编程-Ajax servlet实例
基于OO模版技术的文档Web输出新法
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛