Solved

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

Posted on 2003-11-21
8
392 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
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fisheye tool 2 115
getting param value 7 57
Suggestion on WebSite Template Sites 6 108
null output 3 35
IT certifications are a concrete representation of continual learning on the part of the candidate.  Continual learning is necessary for the long term success of an IT professional, but are IT certifications the right path for you?
ConnectWise and their customers need to ensure critical alerts automatically reach the right person at the right time. MSP superheros efficiently respond to these alerts key is providing automatic, intelligent alerting that generates a complete audi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

792 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