简体   繁体   English

如何在jsp文件中显示Java多线程输出

[英]How to display Java Multi threading output in jsp file

I have a Java Program which contains my own thread (start() and run() methods). 我有一个Java程序,其中包含我自己的线程(start()和run()方法)。 Code is shown below : 代码如下所示:

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 File: 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>

As of now I am displaying the result in java console. 到目前为止,我正在Java控制台中显示结果。 But my task is to display the result in jsp file, Is it possible? 但是我的任务是在jsp文件中显示结果,这可能吗? If possible how to print the data in webpage using jsp file? 如果可能,如何使用jsp文件在网页中打印数据?

创建一个像map或list这样的共享集合,后台线程将填充到共享集合中,并通过AJAX或其他机制从共享集合中获取。考虑线程安全性,因为它需要多个线程在处理后台线程时访问该集合。

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

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