I created an Arduino device that converts data every time mail comes in. The data is sent to the Web server below by Arduino using Wi-Fi.
<%@ 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> <strong>Time</strong> </td><td> <strong>Mail</strong> </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> "+time+" </td><td> Letter: "+stack+" </td></tr>");
}
}catch(SQLException e){
e.printStackTrace();
}
%>
</table>
</body>
</html>
The Java Fx GUI, which appears whenever data is entered into a table on the Web server, must change the value in real time, but it does not change.
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;
}
}
The above is Jsoup, the parsing class, and the following is the controler of Java fx.
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();
};
}
I also tried parsing using selenium, but the results were the same. Parsing certain data has succeeded, but the value does not change in real time. Is there a good any way?
I'm not sure how Java fx controller works but I assume that only one instance of Controller will be created and if that is correct then you create Mail object only once. So you trying to call getMail() object every time on the same object. Since you requesting index.jsp from your Mail class during object construction (i.e. in Mail contructor) the only moment when you actually read the data from index.jsp is when Mail object creating. And from the above assumption it happens only once, when fx controller is being loaded. So the easiest way to achieve what you want is to move the code from Mail constructor to the getMail() method.
public String getMail() {
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");
return cols.get(1).text();
}catch(IOException e) {
System.out.println(e.getMessage());
System.out.println(e.toString());
return "";
}
}
But be aware that the whole code is looking ugly and non-obvious so it can be used just for a quick try.