Solved

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

Posted on 2003-11-21
8
385 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
grep code 4 154
Getting NoSuchMethodError on accessing a spring mvc project 3 147
own marker interface 1 44
struts spring hibernate example 12 86
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now