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

AJAX学习总结(八)---Jquery实例:动态股票价格

在上一个项目的基础上学习该实例:

?

步骤1.新建bean

package bean;
/**
 * 用于保存股票的基本信息
 */
public class Stock {
	//昨天的收盘价
	private double yesterday;
	//今天的开盘价
	private double today;
	//当前价格
	private double now;
	//股票名称
	private String name;
	//股票代码
	private String id;
	
	public Stock(){}
	
	public Stock(double yesterday, double today, String name, String id) {
		super();
		this.yesterday = yesterday;
		this.today = today;
		this.name = name;
		this.id = id;
		this.now = today;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getNow() {
		return now;
	}
	public void setNow(double now) {
		this.now = now;
	}
	public double getToday() {
		return today;
	}
	public void setToday(double today) {
		this.today = today;
	}
	public double getYesterday() {
		return yesterday;
	}
	public void setYesterday(double yesterday) {
		this.yesterday = yesterday;
	}
	public String toString(){
		return this.name + ":" + this.now;
	}
}

?

?

?

步骤2.新建servlet

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.Stock;
/**
 * 返回股票当前信息的servlet
 */
public class GetStocksInfo extends HttpServlet {
	private static final long serialVersionUID = -8644886633753734374L;
	//保存股票信息的map
	private HashMap<String, Stock> stocks;
	public 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.将随机数和股票当前价格进行加或者减的操作,得到新的当前价格
		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);
		
		
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		//out.println(szzs + "<br/>" + pfyh);
		//3.返回两只股票的昨天收盘,今天开盘和当前价格
		//采用json的数据格式返回股票的信息
		StringBuilder builder = new StringBuilder();
		//采用数组方式返回两个股票对象
		/*
		builder.append("[{name:\"").append(szzs.getName()).append("\",id:\"").append(szzs.getId())
				.append("\",yes:").append(szzs.getYesterday()).append(",tod:").append(szzs.getToday())
				.append(",now:").append(szzs.getNow())
				.append("},")
				.append("{name:\"").append(pfyh.getName()).append("\",id:\"").append(pfyh.getId())
				.append("\",yes:").append(pfyh.getYesterday()).append(",tod:").append(pfyh.getToday())
				.append(",now:").append(pfyh.getNow())
				.append("}],");
				*/
		//采用对象的方式回传两个股票对象
		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.ge