Srinivas
Srinivas

Reputation: 15

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?

Upvotes: 1

Views: 743

Answers (1)

gati sahu
gati sahu

Reputation: 2641

Create a shared collection like map or list .The background thread will populate into the shared collection.And through AJAX or other mechanism fetch from the shared collection.Consider thread safety as it required multiple thread will access the collection while background thread is processing.

Upvotes: 2

Related Questions