我有一个Java程序,它包含我自己的线程(start()和run()方法)。代码如下所示:
public class MyClass
{
public void threadStart()
{
Threadone t1 = new Threadone();
t1.start();
}
}
class Threadone extends Thread
{
public void run()
{
String url = "jdbc:mysql://localhost:3306";
String user= "root";
String pwd = "root@123";
String qry = "SELECT Country FROM test.abuse WHERE Priority='High'";
Connection conn = null;
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, user, pwd);
ResultSet rs = conn.prepareStatement(qry).executeQuery();
while(rs.next())
{
System.out.println(rs.getString("Country"));
}
}
catch (SQLException | ClassNotFoundException e)
{
e.printStackTrace();
}
}
}
JSP文件:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
MyClass my = new MyClass();
my.threadStart();
%>
</body>
</html>
截至目前,我正在java控制台中显示结果。但我的任务是在jsp文件中显示结果,是否可能?如果可能,如何使用jsp文件在网页中打印数据?
答案 0 :(得分:2)
创建一个共享集合,如map或list。后台线程将填充到共享集合中。并通过AJAX或其他机制从共享集合中获取。考虑线程安全,因为它需要多个线程将访问集合,而后台线程是处理