Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

servlet question

Status
Not open for further replies.

robF

Programmer
Dec 15, 2000
8
US
Hi,

I have written a method that gets a 'result set' from a SQL query and saves it as a .txt file in a specific directory on the server.

When a visitor to the site clicks the button which files off the servlet, it 'generates' the file and 'dates' it up to the second.

My question is...is it possible to not only archive the file, but have it automatically kick back to the browser giving the surfer an option to download it?

If that isn't possible, I was thinking of just making another link referencing the .txt file that the surfer could just click. However, with several of the files archived in the directory, how to I make it so they get the latest generated file. (the data generated changes constantly)

Thanks for any help :)
Rob
 
hi,

I think you can follow this logic :

String fileNamePattern = "txtFileName" );
File saveDir = new File ( "txtFileDir" ) ;
Stirng[] fileNames = saveDir.list();
int latestFileIndex = -1 ;
long lastModified = -1 ;

for ( int i = 0 ; i < fileNames.length ; i++ ) {
File f = new File(fileNames);
if ( f.isFile() && f.startsWith(fileNamePattern)
&& f.lastModified() > lastModified ) {
lastModified = f.lastModified() ;
} else {
continue ;
}
}

File txtFile = null;

if ( latestFileIndex == -1 ){
txtFile = retrieveFromDBAndMakeFile();
} else {
txtFile = new File(fileNames[latestFileIndex]);
}


As for poping up a window to save the txt file you can add some javascript code that will open a window with the url to filename and can put a call to that method in the onLoad method. eg.

<BODY onLoad=&quot;openTxtFile();&quot;>
</BODY>

venky
 
Thanks Venky!

I appreciate your help very much, I think this will work :)

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top