Terminate a jsp page on exit in browser

I have a jsp page that reads from an excel file as a database. I have it sleep everytime it finish reading a row and prints it out. I do this to make it a really long task.

My problem is when i exit my jsp page on a browser it seem like the page is still running and reading the file. Either the jsp page is or Tomcat is, but all i know I can't delete the excel file once I exit the jsp page.

I can however wait till the jsp page finish and delete the file but that is not always the case.

My question is, is there a way to terminate the page upon exiting the page or anyway to terminate the page once it is running.

Thank you,

Henry

[671 byte] By [geiz777a] at [2007-11-27 10:26:59]
# 1

It looks like the file is in use, so you could not delete it. If your worry is about security, then you can protect the excel sheet so that no one can open the file. sounds good?

OR, Why don't you dynamically delete the file after jsp finish running?

skp71a at 2007-7-28 17:42:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

What is the scenario here?

The user has gotten too impatient and closed their browser before the page is fully loaded?

The user has navigated elsewhere before the page is fully loaded?

I'm not sure if the server can detect either of these conditions, so it would just keep running and reading the Excel file.

What is the reason for making it sleep - just so you can test this functionality?

I don't know of any easy way to terminate a page externally. You could probably code it into your reading process to check a flag on every row, and if that flag is set terminate the current process.

Hope this helps,

evnafets

evnafetsa at 2007-7-28 17:42:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> It looks like the file is in use, so you could not

> delete it. If your worry is about security, then you

> can protect the excel sheet so that no one can open

> the file. sounds good?

> OR, Why don't you dynamically delete the file after

> jsp finish running?

Yeah I think it is still in use because the jsp page didn't finish reading from the excel file. What i wanted to do was to have the page stop reading the file when it is terminated prematurely.

geiz777a at 2007-7-28 17:42:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

> What is the scenario here?

>

> The user has gotten too impatient and closed their

> browser before the page is fully loaded?

> The user has navigated elsewhere before the page is

> fully loaded?

>

> I'm not sure if the server can detect either of these

> conditions, so it would just keep running and reading

> the Excel file.

>

> What is the reason for making it sleep - just so you

> can test this functionality?

>

> I don't know of any easy way to terminate a page

> externally. You could probably code it into your

> reading process to check a flag on every row, and if

> that flag is set terminate the current process.

>

> Hope this helps,

> evnafets

Scenario: I upload an excel file to the server and save it on the server and connect to it with some code from the jsp page to read from it and output the data.

I put the sleep to give me time to exit the jsp page before it finish and test to see if the file is still in use. And it turn out it is still in use.

I though when you exit the browser when a jsp page either it is loaded or while loading it would terminate all task associated with the page.

geiz777a at 2007-7-28 17:42:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> I though when you exit the browser when a jsp page

> either it is loaded or while loading it would

> terminate all task associated with the page.

The page is closed/ browser is exited on the client side; there is no notification sent to the server about this. Also, the browser doesn't hold the locks on the file, the server ( Tomcat in your case? ) does. And since it has no idea that browser was closed, it continues to hold the locks on that file.

I don't know if this'll work but how about a finally block? Since it's supposed to run in all cases, you could put the complete page contents in a try and then a finally to close the connection and delete the file. I have no idea if this is good practice or not, but it just might work for you...

nogoodatcodinga at 2007-7-28 17:42:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

do a try/catch with a finally block, in this finally block close the filestream if it's still open

radicjesa at 2007-7-28 17:42:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

That's what I said! :D

nogoodatcodinga at 2007-7-28 17:42:49 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

Thats only work if the user stays on the page till it finish. But in my case the use exit in the middle of the process. So tomcat just keep running the jsp page.

geiz777a at 2007-7-28 17:42:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

> Thats only work if the user stays on the page till it

> finish. But in my case the use exit in the middle of

> the process. So tomcat just keep running the jsp

> page.

The only way I know of for a server to know that a client left the site is if the client tells the server s/he is leaving. That means a javascript hidden on the page that pings the server on a semi-regular pace.

The server would have to read these pings and put a flag in the user's session - easiest way is to use timestamp (either from when the last ping was made, or when the next ping is expected).

You would have to put the Excel reading into a loop, where the condition to continue with the loop is based on the timestamp OR the contents of the file. The loop pseudo-code might look like:

do {

row = excelFile.getNextRow();

writeToDB(row);

wait(5000);

} while ( timestampIsCurrent() && dataLeftInFile(excelFile))

Of course you should also have the try{}finally{} blocks to make sure the streams are closed.

stevejlukea at 2007-7-28 17:42:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

> > Thats only work if the user stays on the page till

> it

> > finish. But in my case the use exit in the middle

> of

> > the process. So tomcat just keep running the jsp

> > page.

>

> The only way I know of for a server to know that a

> client left the site is if the client tells the

> server s/he is leaving. That means a javascript

> hidden on the page that pings the server on a

> semi-regular pace.

>

> The server would have to read these pings and put a

> flag in the user's session - easiest way is to use

> timestamp (either from when the last ping was made,

> or when the next ping is expected).

>

> You would have to put the Excel reading into a loop,

> where the condition to continue with the loop is

> based on the timestamp OR the contents of the file.

> The loop pseudo-code might look like:

> code]

> do {

>row = excelFile.getNextRow();

> writeToDB(row);

>wait(5000);

> while ( timestampIsCurrent() &&

> dataLeftInFile(excelFile))

> [/code]

>

> Of course you should also have the try{}finally{}

> blocks to make sure the streams are closed.

Thanks I'll try it out.

geiz777a at 2007-7-28 17:42:50 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...