Ajax的应用4模拟股票涨停,采用json传递对象

Posted on 2009-07-18 21:21  哥德巴赫猜  阅读(535)  评论(0)    收藏  举报

有一个javabean : Stock.java

有一个Servlet: TestStockServlet.java

有一个 页面:TestStock.html

Js:           jquerystock.js

 

Stock.java

/**

 * Created by IntelliJ IDEA.

 * User: wenbing lu

 * Date: 2009-7-18

 * Time: 9:22:48

 * To change this template use File | Settings | File Templates.

 */

public class Stock {

    private String name;

    private String id;

    private double yestdayPrice;

    private double todayPrice;

    private double nowPrice;

 

    public Stock(String name, String id, double yestdayPrice, double todayPrice) {

        this.name = name;

        this.id = id;

        this.yestdayPrice = yestdayPrice;

        this.todayPrice = todayPrice;

        this.nowPrice =  todayPrice;

    }

 

  

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public String getId() {

        return id;

    }

 

    public void setId(String id) {

        this.id = id;

    }

 

    public double getYestdayPrice() {

        return yestdayPrice;

    }

 

    public void setYestdayPrice(double yestdayPrice) {

        this.yestdayPrice = yestdayPrice;

    }

 

    public double getTodayPrice() {

        return todayPrice;

    }

 

    public void setTodayPrice(double todayPrice) {

        this.todayPrice = todayPrice;

    }

 

    public double getNowPrice() {

        return nowPrice;

    }

 

    public void setNowPrice(double nowPrice) {

        this.nowPrice = nowPrice;

    }

 

    public String toString(){

        return this.name + ": " + this.nowPrice;

    }

 

}

 

TestStockServlet.java

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletException;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.HashMap;

 

/**

 * Created by IntelliJ IDEA.

 * User: wenbing lu

 * Date: 2009-7-18

 * Time: 9:32:34

 * To change this template use File | Settings | File Templates.

 */

public class TestStockServlet extends HttpServlet {

   

    private HashMap<String,Stock> Stocks;

    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {

       doGet(httpServletRequest, httpServletResponse);

    }

 

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置随机数

        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;

 

        Stock szzs = Stocks.get("00001");

        Stock pufa = Stocks.get("00002");

        double temp;

        //对股票当前指数进行加减

        if(flagsz)  {

            temp = szzs.getNowPrice() + sz;

        }else{

            temp = szzs.getNowPrice() - sz;

        }

        //取小数点后两位

        temp = ((int)(temp * 100) /100.0);

        szzs.setNowPrice(temp);

        if(flagpf)  {

            temp = pufa.getNowPrice() + pf;

        }else{

            temp = pufa.getNowPrice() - pf;

        }

        temp = ((int)(temp * 100) /100.0);

        pufa.setNowPrice(temp);

 

        response.setContentType("text/html;charset=gbk");

        PrintWriter out = response.getWriter();

        //out.println(szzs + "<br/>" + pufa);

        //采用json的数据格式返回股票信息

        StringBuilder builder = new StringBuilder();

        //数组的方式回传两个股票对象

       /* builder.append("[{ name:\" ").append(szzs.getName()).append(" \",")

               .append("id: \"").append(szzs.getId()).append(" \",")

               .append("yes: ").append(szzs.getYestdayPrice()).append(" ,")

               .append("tod: ").append(szzs.getTodayPrice()).append(" ,")

               .append("now: ").append(szzs.getNowPrice()).append(" },")

               .append("{ name: \"").append(pufa.getName()).append(" \",")

               .append("id: \"").append(pufa.getId()).append(" \",")

               .append("yes: ").append(pufa.getYestdayPrice()).append(" ,")

               .append("tod: ").append(pufa.getTodayPrice()).append(" ,")

               .append("now: ").append(pufa.getNowPrice()).append(" }]");

        out.println(builder);*/

 

       /* [{

           name:"szzs.getName()",

            id: "szzs.getId()",

            yes: szzs.getYestdayPrice(),

            tod: szzs.getTodayPrice(),

            now: szzs.getNowPrice()

        },

        {

           name:"pufa.getName()",

            id: "pufa.getId()",

            yes: pufa.getYestdayPrice(),

            tod: pufa.getTodayPrice(),

            now: pufa.getNowPrice()

        }]

*/     //对象的方式回传两个股票对象

        /*{

            szzs.getId():

            {

            name:"szzs.getName()",

            yes: szzs.getYestdayPrice(),

            tod: szzs.getTodayPrice(),

            now: szzs.getNowPrice()

            },

            pufa.getId():

            {

            name:"pufa.getName()",

            yes: pufa.getYestdayPrice(),

            tod: pufa.getTodayPrice(),

            now: pufa.getNowPrice() 

            }

       /* }*/

        //,如果采用对象的方式返回对象,要在对象上加括号

        builder.append("({ \"").append(szzs.getId()).append("\" : {")

               .append("name: \"").append(szzs.getName()).append(" \",")

               .append("yes: ").append(szzs.getYestdayPrice()).append(" ,")

               .append("tod: ").append(szzs.getTodayPrice()).append(" ,")

               .append("now: ").append(szzs.getNowPrice()).append(" },\"")

               .append(pufa.getId()).append("\" : {")

               .append("name: \"").append(pufa.getName()).append(" \",")

               .append("yes: ").append(pufa.getYestdayPrice()).append(" ,")

               .append("tod: ").append(pufa.getTodayPrice()).append(" ,")

               .append("now: ").append(pufa.getNowPrice()).append(" }})");

        out.print(builder);

    }

 

    @Override

    public void init() throws ServletException {

        Stocks = new HashMap<String,Stock>();

        Stock szzs = new Stock("上证指数","00001",3000.0,2999.0);

        Stock pufa = new Stock("浦发银行","00002",25.0,24.0);

        Stocks.put(szzs.getId(),szzs);

        Stocks.put(pufa.getId(),pufa);

        System.out.println(Stocks);

    }

}

 

TestStock.html

<html>

<head>

    <title></title>

    <script type="text/javascript" src="jslib/jquery.js"></script>

    <script type="text/javascript" src="jslib/jquerystock.js"></script>

</head>

<body>

      <div id="00001"><a href="#">上证指数:</a><span></span></div>

      <div id="00002"><a href="#">浦发银行:</a><span></span></div>

</body>

</html>

 

 

jquerystock.js

$(document).ready(function(){

 

    getInfo();

    //每隔一秒和服务器进行交互

    setInterval(getInfo,1000);

 

});

 

function getInfo(){

    //向服务器端发起请求,获取数据

     $.get("TestStockServlet",null,function(data){

        //接收数据, 解析数据

        var obj = eval(data);

        //获取两个对象

        var szzs = obj["00001"];

        var pufa = obj["00002"];

 

        //找到页面对应的节点,填充股票价格

       var span1 =  $("#00001").children("span");

         span1.html(szzs.now);

         if(szzs.now > szzs.yes){

             span1.css("color","red");

         }else{

             span1.css("color","green");

         }

       var span2 =  $("#00002").children("span");

         span2.html(pufa.now);

         if(pufa.now > pufa.yes){

             span2.css("color","red");

         }else{

             span2.css("color","green");

         }

    })

}

博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3