简体   繁体   中英

How to display Java Multi threading output in jsp file

I have a Java Program which contains my own thread (start() and run() methods). 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:

 <%@ 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. But my task is to display the result in jsp file, Is it possible? If possible how to print the data in webpage using jsp file?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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