我想继续更新用Java解析的html数据

时间:2019-03-25 16:57:18

标签: jsp

我使用Jsoup来解析我的网页表。已成功读取并显示在我的Java fx gui上。但是,即使有新数据输入,GUI的显示也不会改变。

public class Mail {
String mail;
public Mail() {
    try {
        Document doc = Jsoup.connect("http://localhost14999/stackserver/index.jsp").get();
        Element table = doc.select("table").get(0);
        Elements rows = table.select("tr");

        Element row = rows.get(1);
        Elements cols = row.select("td");

        mail=cols.get(1).text();
        }catch(IOException e) {
            System.out.println(e.getMessage());
            System.out.println(e.toString());
        }
}
public String getMail() {
    return mail;
}
}

这是Javafx的控制器

public class Controller implements Initializable {
    @FXML private Label mail;
    private Mail mt=new Mail();

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        Thread thread = new Thread() {
            @Override
            public void run() {                     
                while(true) {
                    String wfmail = mt.getMail();
                    Platform.runLater(()->{         
                        mail.setText(wfmail);
                    });
                    try {Thread.sleep(1000);}catch(InterruptedException e) {}
                }
            }
        };
        thread.setDaemon(true);
        thread.start();
    };
}

这是一个Web服务器。

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Mail Log</title>
</head>
<body>
	<table id="mail" align="center" border="0" cellspacing="1" cellpadding="3">
		<tr align="center">
			<td>&nbsp;<strong>Time</strong>&nbsp;</td><td>&nbsp;<strong>Mail</strong>&nbsp;</td>
		</tr>
	<%
		Connection conn=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try{
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/STACK","root","mingky1218");
			ps=conn.prepareStatement("SELECT * FROM MESSAGE ORDER BY TIMESTAMP DESC");
			rs=ps.executeQuery();
			while(rs.next()){
				String time=rs.getString("timeStamp");
				String stack=rs.getString("STACK");
				out.println("<tr><td>&nbsp;"+time+"&nbsp;</td><td>&nbsp;Letter: "+stack+"&nbsp;</td></tr>");
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	%>
	</table>
</body>
</html>

我在Mail类中应用了可运行线程,但是失败了。 我想不断地更新数据。 有什么好方法吗?

0 个答案:

没有答案