日期:2014-05-16  浏览次数:20625 次

Ajax学习笔记03-模拟动态显示股票信息动态变化的小例子
模拟实现一个动态显示股票价格的经典例子:

实体类 :Stock.java  
由于篇幅问题,此处省略了get Set 方法
/**
 * Created by IntelliJ IDEA.
 * User: 赵光鹏
 * Date: 2011-11-24
 * Time: 10:51:15
 * To change this template use File | Settings | File Templates.
 */
public class Stock {
        private double yesterday;
        private double today ;
        private double  now;
  	     private String name;
        private String id;

    public Stock( double yesterday, double today, String name, String id){
       this.yesterday = yesterday;
        this.today = today;
        this.name = name;
        this.id = id;
        this.now = today;
    }
    public String toString(){
        return this.name+":"+this.now;
    }
}

Servlet: GetStocksInfo.java

import ...


/**
 * Created by IntelliJ IDEA.
 * User: 赵光鹏
 * Date: 2011-11-24
 * Time: 10:52:01
 * To change this template use File | Settings | File Templates.
 */
public class GetStocksInfo extends HttpServlet {
    private HashMap<String,Stock> stocks;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doGet( request,  response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //返回两只股票的价格信息
        //1.计算随机数
        double sz = Math.random() + 20;
        double pf = Math.random() + 0.5;

        //通过随机数是奇数还是偶数来判断股票上涨还是下跌
        boolean flagsz = ((int)(Math.random() * 10))%2 ==0;
         boolean flagpf = ((int)(Math.random() * 10))%2 ==0;

        //2.将随机数和当前价格进行加减的操作
          //2.1首先获取到先前实例化好的两个数据
         Stock szzs = stocks.get("300001");
         Stock pfyh = stocks.get("000001");

        double temp;
        if(flagsz){
           temp = szzs.getNow() + sz;
        }  else{
             temp = szzs.getNow() - sz;
        }
        //对新的书进行小数位数进行处理,只保留小数点后后两位
        temp = (int)(temp*100)/100.0;
        szzs.setNow(temp);
      if(flagpf){
           temp = pfyh.getNow() + pf;
        }  else{
             temp = pfyh.getNow() - pf;
        }
        temp = (int)(temp*100)/100.0;
        pfyh.setNow(temp);
        //采用json数据格式返回股票的信息
        StringBuilder builder = new StringBuilder();
        //采用数组的形式 在此处略过
        //采用对象的方式回传两个股票对象
        //前台效果:{
        //           300001:{name:"上证指数",yes:3000.0",tod:2990.0,now:3010.02},
        //           000001:{name:"浦发银行",yes:23.22",tod:23.55,now:24.4}
        //          }
        builder.append("({")
                .append("\"")
               .append(szzs.getId()).append("\":{name:\"").append(szzs.getName())
                .append("\",yes:").append(szzs.getYesterday()).append(",tod:").append(szzs.getToday())
                .append(",now:").append(szzs.getNow())
                .append("},")  .append("\"")
                .append(pfyh.getId()).append("\":{name:\"").append(pfyh.getName())
                .append("\",yes:").append(pfyh.getYesterday()).append(",tod:").append(pfyh.getToday())
                .append(",now:").append(pfyh.getNow())
                .append("}})");

        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        out.print(builder);
    }
    //在服务器启动时(配置文件中配置好),执行该方法,虚拟出两条股票信息
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        stocks = new HashMap<String, Stock>();
        Stock szzs = new Stock(3000.0, 2990.0, "上证指数", "300001");
        Stock pfyh = new Stock(23.22, 23.55, "浦发银行", "000001");
        //将两支股票保存在Stock的Map中
        stocks.put(szzs.getId(),szzs);
        stocks.put(pfyh.getId(),pfyh);

        System.out.println(stocks);
    }
}


Html页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <script type="text