Solved

Servlet has wait for file to be generated, but how? thread?

Posted on 2003-11-21
8
398 Views
Last Modified: 2010-04-01
hi, i have the following problem:
I´m using a servlet to provide downloads.
The files that the user wants to download are generated on the fly.
So, the user klickes a link to the servlet,
the servlet calls some functions to generate the file.
when the file is ready, i get a <filename>.go file.
in the servlet i wait for that file to appear.
i do that by:
        File gofile = new File(<pathtofile>);
        while (!gofile.exists())
        {
        }
thats not good i know, but it works. :-)
I tried to use sleep and wait functions, but i get something like: you are not the owner of the thread.
How can i make an servlet to run in an own thread that i can set to sleep(<sometime>) to check if the file exists every second without blocking the whole system.

by the way is there a way to reload the page that called the servlet?

thx, for any solutions or hints.

0
Comment
Question by:Koma666
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 125 total points
ID: 9796184
> How can i make an servlet to run in an own thread that i can set to sleep(<sometime>) to check if the file exists every second without blocking the whole system.

Try:

final File gofile = new File( "thefile.txt" ) ;
Thread checker = new Thread()
                          {
                             public void run()
                             {
                                 while( !gofile.exists() )
                                 {
                                    try { Thread.sleep( 1000 ) ; } catch( InterruptedException ex ) { }
                                 }
                             }
                          } ;
checker.start() ;
checker.join() ;
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9796191
> by the way is there a way to reload the page that called the servlet?

Not sure what you mean....what is the situation you want this to happen?
0
 
LVL 5

Author Comment

by:Koma666
ID: 9796225
>> by the way is there a way to reload the page that called the servlet?
>Not sure what you mean....what is the situation you want this to happen?

the user is on a page with the link to the download servlet
he klick the link.
lets say href='servlet/download?file=something'
so no new HTML page is opened, and i cant reload by something like Javascript:opener.location.reload();
the new page has application/octet-stream in the header.

the servlet waits for the file, then sends the file to the user.
after sending is complete the servlet deletes the file on the server.
after that i set the last download time.

on the page with the servlet link the last download time is shown, so i have to reload the page, because the time has changed.

i hope i explained it right. :-)

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 9796245
Can you write the file to the outputstream, then do a:

response.sendRedirect() ?

Not sure if you can...
0
 
LVL 5

Author Comment

by:Koma666
ID: 9796260
no, that dosn´t work.
0
 
LVL 5

Author Comment

by:Koma666
ID: 9796296
but the thread thing was exactly what i needed.
thx for the fast help.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9796298
Hmmmm...   Yeah...IllegalState...

I think you are going to have to use some javascript jiggery-pokery to get this working...

can you have the link for the download pop-up a window (that does the download), and then use javascript to refresh the current page?

if you increment the counter before all of the Thread stuff, then the counter should have increased?

Tim
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9796300
>  but the thread thing was exactly what i needed.

Glad I could help with the main problem :-)

Good luck!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
This article outlines the struggles that Macs encounter in Windows-dominated workplace environments – and what Mac users can do to improve their network connectivity and remain productive.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question