In my web application, I have a JSP page to which allows a user to upload a CSV file.
When user submits this upload form, control goes to a Struts2 action,
from this action I am starting a new Java thread which handles the reading and processing of this CSV.
I don't want to block the view of application as CSV can be very large so thread handles the uploading and reading of CSV
and action returns to some confirmation view page.
Now I want to notify user when thread completes its execution and uploading of file is done.
As soon as thread finishes its execution, I want to pop a javascript alert in my web app
with a message "Upload complete. Click here to view".
My current approach(not good) is to pass session object to thread and set a completion flag in thread.
Action
//----
new Thread(new UploadThread(session)).start();
//----
UploadThread.java
//----
try {
//Reading and processing CSV
} catch() {
//Exception handling
} finally {
//Set flag in every case
session.setAttribute("uploadFlag","true");
}
//----
In the meantime, I set a JS method from one of my JSPs to execute in every 5 seconds. In every 5 secs, this method checks "uploadFlag" from session and if its value is set, it pops javascript alert.
This is working but session object should not be in Thread. Is there some way to achieve this alert from Thread's finally block.
I did some googling and found these SO posts- Open local html page - java and Getting java gui to open a webpage in web browser
Apart from these I've tried to use java.net.URL + openConnection and HttpClient also.
But all these return output of target URL in stream.
Kindly suggest.