简体   繁体   English

高图-从Servlet到JSP

[英]Highcharts - From Servlet to JSP

I have some problems with Highcharts and servlet/jsp. 我对Highcharts和servlet / jsp有一些问题。

I'm not very familiar with AJAX and JQUERY. 我对AJAX和JQUERY不太熟悉。

I would like to display in a jsp page 2 real-time line series with the relevant data (x,y) loaded from a database through a java servlet. 我想在jsp页面2实时行系列中显示通过Java servlet从数据库加载的相关数据(x,y)。

This is the code inside my jsp: 这是我的jsp中的代码:

<script type="text/javascript">
$(function () {

    var seriesOptions = [];
    $.getJSON('Grafico', function(data) {

        // Populate series
        for (i = 0; i < data.DeltaRealTime.length; i++){
            seriesOptions[i] = {
                name: data.DeltaRealTime[i].name,
                data: [data.DeltaRealTime[i].key, parseFloat(data.DeltaRealTime[i].value)]
            };
        }

        $('#container').highcharts({
            chart: {
                type: 'spline',
                animation: Highcharts.svg, // don't animate in old IE
                marginRight: 10,

                events: {
                    load: function() {

                        // set up the updating of the chart each second
                        setInterval(function() {

                            $.getJSON('Grafico', function(data) {
                                // Populate series
                                for (i = 0; i < data.DeltaRealTime.length; i++){
                                    var chart = $('#container').highcharts();
                                    chart.series[i].addPoint([data.DeltaRealTime[i].key, parseFloat(data.DeltaRealTime[i].value)]);
                                }
                            });
                        }, 1000);
                    }
                }
            },
            title: {
                text: "Valori Delta"
            },
            xAxis: {
                title: {
                    text: 'Campione'
                }
            },
            yAxis: {
                labels: {
                    format: '{value:.1f}'
                },
                title: {
                    text: 'Delta(f)'
                },
            },
            series: seriesOptions
        });
    });
});
</script>

This is the "Grafico" servlet: 这是“ Grafico” servlet:

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

    response.setContentType("text/json");
    PrintWriter out = response.getWriter();

    try {

        HttpSession session = request.getSession();

        Seriale seriale = (Seriale)session.getAttribute("seriale");

        if(seriale == null)
            System.out.println("SERIALE=NULL");
        else {
            if(seriale.getSeriale().isOpen())
                System.out.println("SERIALE APERTA");

            System.out.println("ID SESSIONE DATI="+seriale.getSessioneDati().getId());
        }


        if(seriale != null && seriale.getSeriale().isOpen()) {

            System.out.println("INIZIO A LEGGERE I DATI DAL DB");

            Db db = new Db();
            db.apriConnessione();

            String sql = "SELECT * FROM MisuraRiposo ORDER BY id DESC LIMIT 1";
            PreparedStatement ps = db.getConnection().prepareStatement(sql);

            ResultSet rs = ps.executeQuery();

            Map<String, String> data1 = new HashMap<String,String>();
            Map<String, String> data2 = new HashMap<String,String>();

            if(rs.next()) {
                data1.put("name", "f1");
                data1.put("key", rs.getString("campione"));
                data1.put("value", rs.getString("f1"));
                data2.put("name", "f2");
                data2.put("key", rs.getString("campione"));
                data2.put("value", rs.getString("f2"));
            }

            rs.close();
            ps.close();
            db.chiudiConnessione();

            JSONObject json1 = new JSONObject(data1);
            JSONObject json2 = new JSONObject(data2);
            JSONArray array = new JSONArray();
            array.put(json1);
            array.put(json2);
            JSONObject finalObject = new JSONObject();
            finalObject.put("DeltaRealTime", array);

            out.write(finalObject.toString());
        }

    }
    catch(JSONException jse) {
        jse.printStackTrace();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    finally {
        out.flush();
        out.close();

        System.out.println("Fine servlet grafico");
    }

}

The problem is that I can't obtain the desired behaviour. 问题是我无法获得所需的行为。 I only obtain disconnected points. 我只获得断开连接的点。

Thanks in advance for any help :) 在此先感谢您的帮助:)

I managed to display in a jsp page 12 real-time line series with the relevant data (x,y) loaded from a database through a java servlet. 我设法在jsp页面12中显示了实时行系列,其中包含通过Java servlet从数据库加载的相关数据(x,y)。 The series are updated once a second. 该系列每秒更新一次。 All works very well in Chrome and Firefox. 在Chrome和Firefox中,所有功能都运行良好。 Instead I'm experiencing problems with Internet Explorer 11.0.9600.17239 where the chart does not update (it remains on the first chart image forever). 相反,我遇到Internet Explorer 11.0.9600.17239的问题,该图表不会更新(它永远保留在第一个图表图像上)。 However, if I change to compatibility view (F12), the chart works very well also in Internet Explorer. 但是,如果我更改为兼容性视图(F12),则该图表在Internet Explorer中也可以很好地工作。 I can't understand why. 我不明白为什么。 Can you help me? 你能帮助我吗? Sorry if the code is not very elegant. 很抱歉,如果代码不是很优雅。

This is the code inside my jsp: 这是我的jsp中的代码:

<script type="text/javascript">
$(document).ready(function() {
//fare cose jQuery quando DOM è pronto
Highcharts.setOptions({
    global: {
        useUTC: false
    }
});

    var processed_json = [];
    $.getJSON('Grafico', function(data) {

        // draw chart
        $('#container').highcharts({
        chart: {
            zoomType: 'x',
            type: "spline",
            //animation: Highcharts.svg, // don't animate in old IE
            events: {
                load: function() {

                    // set up the updating of the chart each second
                    setInterval(function() {

                        $.getJSON('Grafico', function(data) {
                            // Populate series
                            for (i = 0; i < data.DeltaRealTime.length; i++){
                                var chart = $('#container').highcharts();
                                chart.series[i].addPoint([parseInt(data.DeltaRealTime[i].key),parseFloat(data.DeltaRealTime[i].value)]);
                            }
                        });
                    }, 1000);
                }
            }
        },
        title: {
            text: "Valori Delta"
        },
        xAxis: {
            title: {
                text: 'Campione'
            },
            labels: {
                step: 1
            }
        },
        yAxis: {
            labels: {
                format: '{value:.1f}'
            },
            title: {
                text: 'Delta(f)'
            }
        },
        series: [{
            name: 'f1',
            data: []
        }, {
            name: 'f2',
            data: []
        }, {
            name: 'f3',
            data: []
        }, {
            name: 'f4',
            data: []
        }, {
            name: 'f5',
            data: []
        }, {
            name: 'f6',
            data: []
        }, {
            name: 'f7',
            data: []
        }, {
            name: 'f8',
            data: []
        }, {
            name: 'f9',
            data: []
        }, {
            name: 'f10',
            data: []
        }, {
            name: 'f11',
            data: []
        }, {
            name: 'f12',
            data: []
        }]
    });
});

}); });

This is the "Grafico" servlet: 这是“ Grafico” servlet:

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

    doPost(request, response);

}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.out.println("Avvio servlet grafico");

    Map<String, String> f1 = new HashMap<String,String>();
    Map<String, String> f2 = new HashMap<String,String>();
    Map<String, String> f3 = new HashMap<String,String>();
    Map<String, String> f4 = new HashMap<String,String>();
    Map<String, String> f5 = new HashMap<String,String>();
    Map<String, String> f6 = new HashMap<String,String>();
    Map<String, String> f7 = new HashMap<String,String>();
    Map<String, String> f8 = new HashMap<String,String>();
    Map<String, String> f9 = new HashMap<String,String>();
    Map<String, String> f10 = new HashMap<String,String>();
    Map<String, String> f11 = new HashMap<String,String>();
    Map<String, String> f12 = new HashMap<String,String>();

    response.setContentType("text/json");
    PrintWriter out = response.getWriter();

    try {

        HttpSession session = request.getSession();
        Seriale seriale = (Seriale)session.getAttribute("seriale");

        if(seriale == null)
            System.out.println("SERIALE=NULL");
        else {
            if(seriale.getSeriale().isOpen())
                System.out.println("SERIALE APERTA");

            System.out.println("ID SESSIONE DATI="+seriale.getSessioneDati().getId());
        }

        Delta delta = new Delta();

        if(seriale != null && seriale.getSeriale().isOpen() && seriale.getPrimaMisura() != null) {

            MisuraCampione primaMisura = new MisuraCampione(); 
            primaMisura = seriale.getPrimaMisura();
            System.out.println("***STAMPA PRIMA MISURA***");
            primaMisura.stampa();

            System.out.println("INIZIO A LEGGERE I DATI DAL DB");

            Db db = new Db();
            db.apriConnessione();

            String sql = "SELECT * FROM MisuraRiposo ORDER BY id DESC LIMIT 1";
            PreparedStatement ps = db.getConnection().prepareStatement(sql);

            System.out.println("QUERY=" + ps.toString());

            ResultSet rs = ps.executeQuery();

            if(rs.next()) {
                MisuraCampione misuraAttuale = new MisuraCampione();
                misuraAttuale.setCampione(rs.getInt("campione"));
                misuraAttuale.setF1(rs.getBigDecimal("f1"));
                misuraAttuale.setF2(rs.getBigDecimal("f2"));
                misuraAttuale.setF3(rs.getBigDecimal("f3"));
                misuraAttuale.setF4(rs.getBigDecimal("f4"));
                misuraAttuale.setF5(rs.getBigDecimal("f5"));
                misuraAttuale.setF6(rs.getBigDecimal("f6"));
                misuraAttuale.setF7(rs.getBigDecimal("f7"));
                misuraAttuale.setF8(rs.getBigDecimal("f8"));
                misuraAttuale.setF9(rs.getBigDecimal("f9"));
                misuraAttuale.setF10(rs.getBigDecimal("f10"));
                misuraAttuale.setF11(rs.getBigDecimal("f11"));
                misuraAttuale.setF12(rs.getBigDecimal("f12"));
                misuraAttuale.setT1(rs.getBigDecimal("t1"));
                misuraAttuale.setT2(rs.getBigDecimal("t2"));
                misuraAttuale.setTo(rs.getBigDecimal("to"));
                misuraAttuale.setFt(rs.getBigDecimal("ft"));
                misuraAttuale.setRh(rs.getBigDecimal("rh"));
System.out.println("***STAMPA MISURA ATTUALE***");
misuraAttuale.stampa();

                delta = delta.calcolaDelta(primaMisura, misuraAttuale);
System.out.println("***STAMPA DELTA***");
delta.stampa();
            }

            rs.close();
            ps.close();
            db.chiudiConnessione();

        }

        System.out.println("CAMPIONE="+delta.getCampione()+"-"+delta.getCampione2());
        System.out.println("F1="+delta.getF1());
        System.out.println("F2="+delta.getF2());

        f1.put("key", Integer.toString(delta.getCampione2()));
        f1.put("value", delta.getF1().toString());
        f2.put("key", Integer.toString(delta.getCampione2()));
        f2.put("value", delta.getF2().toString());
        f3.put("key", Integer.toString(delta.getCampione2()));
        f3.put("value", delta.getF3().toString());
        f4.put("key", Integer.toString(delta.getCampione2()));
        f4.put("value", delta.getF4().toString());
        f5.put("key", Integer.toString(delta.getCampione2()));
        f5.put("value", delta.getF5().toString());
        f6.put("key", Integer.toString(delta.getCampione2()));
        f6.put("value", delta.getF6().toString());
        f7.put("key", Integer.toString(delta.getCampione2()));
        f7.put("value", delta.getF7().toString());
        f8.put("key", Integer.toString(delta.getCampione2()));
        f8.put("value", delta.getF8().toString());
        f9.put("key", Integer.toString(delta.getCampione2()));
        f9.put("value", delta.getF9().toString());
        f10.put("key", Integer.toString(delta.getCampione2()));
        f10.put("value", delta.getF10().toString());
        f11.put("key", Integer.toString(delta.getCampione2()));
        f11.put("value", delta.getF11().toString());
        f12.put("key", Integer.toString(delta.getCampione2()));
        f12.put("value", delta.getF12().toString());

        JSONObject jsonF1 = new JSONObject(f1);
        JSONObject jsonF2 = new JSONObject(f2);
        JSONObject jsonF3 = new JSONObject(f3);
        JSONObject jsonF4 = new JSONObject(f4);
        JSONObject jsonF5 = new JSONObject(f5);
        JSONObject jsonF6 = new JSONObject(f6);
        JSONObject jsonF7 = new JSONObject(f7);
        JSONObject jsonF8 = new JSONObject(f8);
        JSONObject jsonF9 = new JSONObject(f9);
        JSONObject jsonF10 = new JSONObject(f10);
        JSONObject jsonF11 = new JSONObject(f11);
        JSONObject jsonF12 = new JSONObject(f12);

        JSONArray array = new JSONArray();
        if(delta.getCampione() > 0) {
            array.put(jsonF1);
            array.put(jsonF2);
            array.put(jsonF3);
            array.put(jsonF4);
            array.put(jsonF5);
            array.put(jsonF6);
            array.put(jsonF7);
            array.put(jsonF8);
            array.put(jsonF9);
            array.put(jsonF10);
            array.put(jsonF11);
            array.put(jsonF12);
        }

        JSONObject finalObject = new JSONObject();
        finalObject.put("DeltaRealTime", array);

System.out.println("FINALOBJECT=" + finalObject.toString());                
        out.write(finalObject.toString());

    }
    catch(SQLException sqle) {
        sqle.printStackTrace();
    }
    catch(JSONException jse) {
        jse.printStackTrace();
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    finally {
        out.flush();
        out.close();

        System.out.println("Fine servlet grafico");
    }

}

The "Grafico" java servlet only returns to the jsp page the JSON encoded string used by Highcharts to display the 12 real-time series. “ Grafico” java servlet仅将Highcharts用于显示12个实时序列的JSON编码字符串返回到jsp页面。

The JSON string passed to the jsp page is like this: 传递给jsp页面的JSON字符串如下所示:

{"DeltaRealTime":[{"value":"0.1","key":"371"},{"value":"0.1","key":"371"},{"value":"0.0","key":"371"},{"value":"0.0","key":"371"},{"value":"0.0","key":"371"},{"value":"-0.1","key":"371"},{"value":"0.2","key":"371"},{"value":"0.3","key":"371"},{"value":"-0.2","key":"371"},{"value":"0.0","key":"371"},{"value":"0.0","key":"371"},{"value":"0.1","key":"371"}]} { “DeltaRealTime”:[{ “值”: “0.1”, “关键”: “371”},{ “值”: “0.1”, “关键”: “371”},{ “值”: “0.0” “键”: “371”},{ “值”: “0.0”, “键”: “371”},{ “值”: “0.0”, “键”: “371”},{ “值” : “ - 0.1”, “关键”: “371”},{ “值”: “0.2”, “关键”: “371”},{ “值”: “0.3”, “关键”: “371”} ,{ “值”: “ - 0.2”, “关键”: “371”},{ “值”: “0.0”, “键”: “371”},{ “值”: “0.0”, “钥匙” : “371”},{ “值”: “0.1”, “关键”: “371”}]}

Then, the javascript setInterval function updates once a second the graph by adding a new (x,y) point to each series through the function chart.series[i].addPoint([parseInt(data.DeltaRealTime[i].key),parseFloat(data.DeltaRealTime[i].value)]); 然后,javascript setInterval函数通过通过函数chart.series [i] .addPoint([parseInt(data.DeltaRealTime [i] .key))向每个系列添加新的(x,y)点,每秒更新一次图形。 parseFloat(data.DeltaRealTime [I]。价值)]);

The new point is similar to the previous one: 新点与上一点类似:

{"DeltaRealTime":[{"value":"-0.1","key":"372"},{"value":"-0.1","key":"372"},{"value":"-0.1","key":"372"},{"value":"-0.2","key":"372"},{"value":"-0.2","key":"372"},{"value":"-0.4","key":"372"},{"value":"0.0","key":"372"},{"value":"0.1","key":"372"},{"value":"0.1","key":"372"},{"value":"-0.5","key":"372"},{"value":"-0.2","key":"372"},{"value":"-0.1","key":"372"}]} { “DeltaRealTime”:[{ “值”: “ - 0.1”, “关键”: “372”},{ “值”: “ - 0.1”, “关键”: “372”},{ “值”:” -0.1" , “关键”: “372”},{ “值”: “ - 0.2”, “关键”: “372”},{ “值”: “ - 0.2”, “关键”: “372”} ,{ “值”: “ - 0.4”, “关键”: “372”},{ “值”: “0.0”, “键”: “372”},{ “值”: “0.1”, “关键” : “372”},{ “值”: “0.1”, “关键”: “372”},{ “值”: “ - 0.5”, “关键”: “372”},{ “值”:“ - 0.2" , “关键”: “372”},{ “值”: “ - 0.1”, “关键”: “372”}]}

I found the problem. 我发现了问题。 It seems a JQUERY issue on Internet Explorer. 在Internet Explorer上似乎是JQUERY问题。

$.getJSON('Grafico', function(data) { ... }

doesn't work in IE11. 在IE11中不起作用。

I changed it with 我用

$.ajax({
                        type: $('#form1').attr('method'),
                        url: "Grafico",
                        cache:false,
                        dataType:'json',
                        success: function(data){
                            for (var i = 0; i < data.DeltaRealTime.length; i++){
                                chart1.series[i].addPoint([parseInt(data.DeltaRealTime[i].key),parseFloat(data.DeltaRealTime[i].value)]);
                            }
                        }
                    });

and all works very well. 一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM