如何每10秒刷新一次servlet write.out方法?

时间:2012-03-15 19:50:14

标签: java ajax servlets charts

我有一个连接到mysql数据库的servlet,结果集从一个带有数值的列中获取一些值,这些值在google链接中生成一个图表作为结果...这是我的代码为它:

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

            //OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
            //System.out.println("SensorGraph");

            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            //sensor Id
            String id = request.getParameter("id");
            // limit in the query
            String limit = request.getParameter("limit");
            DatabaseConnection db_connection =  new DatabaseConnection()._instance;
            Connection connection = null;
            try {
                connection = db_connection.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            Statement statement;
            ResultSet resultSet;
            try {
                statement = (Statement) connection.createStatement();
                resultSet = statement.executeQuery("select * from Sensor_Entries order by EntryTime asc limit 100");
                String values = "";
                while(resultSet.next()) {
                    String value = resultSet.getString("EntryValue");
                    values += (value) + ",";
                }
                //System.out.println(values);
                values = values.substring(0, values.length()-1);


                String image = "http://chart.apis.google.com/chart?" +
                        "chxr=0,0,1000" +
                        "&chxs=0,676767,12.167,-0.5,l,676767" +
                        "&chxt=y" +
                        "&chs=1000x300" +
                        "&cht=ls" +
                        "&chco=3D7930" +
                        "&chds=0,1000" +
                        "&chd=t:" + values+
                        "&chg=14.3,-1,1,1" +
                        "&chls=2,4,0" +
                        "&chma=0,6%7C3" +
                        "&chm=B,C5D4B5BB,0,0,0" +
                        "&chtt=Sensor+Values" +
                        "&chts=F00D0D,14.5";

                //if(request.getParameter("img") != null)
                    out.write("<img src=\"" + image + "\" />");

                statement = (Statement) connection.createStatement();
                /*resultSet = statement.executeQuery("select * from Sensors join " +
                        "(select * from Sensor_Entries order by EntryTime desc) as Sensor_Entries on " +
                        "(Sensors.SensorID=Sensor_Entries.SensorID) where Sensors.SensorID='" + id + "' " +
                        "group by Sensors.SensorID;");*/

                resultSet = statement.executeQuery("select * from Sensors join " +
                        "(select * from Sensor_Entries order by EntryTime desc) as Sensor_Entries on " +
                        "(Sensors.SensorID=Sensor_Entries.SensorID)"); /*where Sensors.SensorID='" + id + "' " +
                        "group by Sensors.SensorID;");*/

                ResultSetMetaData resultSetMetaData = (ResultSetMetaData) resultSet.getMetaData();
                if(resultSet.next()) {
                    out.write("<table>");
                    for(int i = 1; i < resultSetMetaData.getColumnCount() + 1; i++) {
                        String value = "";
                        int type = resultSetMetaData.getColumnType(i);
                        if(type == Types.INTEGER || type == Types.TINYINT) {
                            value = "" + resultSet.getInt(i);
                        } else if(type == Types.VARCHAR) {
                            value = resultSet.getString(i);
                        } else if(type == Types.TIMESTAMP) {
                            value = resultSet.getTimestamp(i).toString();
                        } else if(type == Types.DOUBLE) {
                            value = "" + resultSet.getDouble(i);
                        }

                        //out.write("<tr>" +
                               // "<td>" + resultSetMetaData.getColumnName(i) + "</td>" +
                               // "<td>" + value + "</td>" +
                                //"</tr>");
                    }
                    out.write("</table></center>");
                }
                else
                    out.write(image);
                out.flush();
                out.close();
            } catch (SQLException ignored) { ignored.printStackTrace();  }
        }

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

}

我如何获得一个新的图形,每10秒钟提供一个我知道价值观会在价值观中发生变化的价值? ...请帮助我...

1 个答案:

答案 0 :(得分:1)

您无法多次刷新ServletOutputStream。它会抱怨。您必须使用某种标识符创建后台处理线程,并使用客户端的ajax获取当前的计算集并进行渲染。