I am working on a Java Servlet which creates a temporary file to use in a session. At the end of the session (ie, user "logout"), I need to remove the temporary file, and then redirect the user to the initial ("login") page. The redirection works fine, but the temporary file remains intact.
I presume it has something to do with the path to the file, but I am not quite sure as to what. I create the file when the session initiates:
String path = request.getSession().getServletContext().getRealPath("/");
File file = File.createTempFile("getter", ".html", new File(path + "/tmp/"));
Then, when closing the session, I do:
file.delete();
I know about file.deleteOnExit()
, but... when do I exit a servlet? Perhaps I am confused, but I would surely appreciate any help! :)
Thank you in advance!
EDIT
So, here come some details:
I am using a servlet, as I said, for the time being without handling sessions. I agree with @Joop that I will need to implement sessions, but for the time being just wanted to do some simple testing.
So, my servlet hagles GET
and POST
requests. I use a flag in the POST
request to call an internal function which instantiates the file (declared in the class as private File file;
) to a new temp file. On consecutive calls, the file gets populated and saved. In the page the user sees, I have an anchor referring to the servlet (to 'this', that is), passing a flag as a parameter, a flag that indicates the 'logout'. Then I call another internal function which deletes the file previously instantiated.
If it is a matter of sesions, I will implement the manager and post my findings.
EDIT 2
I implemented an HttpSessionListener
, and all seems to work fine. Now, on creating the session, I instantiate a file in my previously declared directory (note that it is not a temp file, I use File file = new File(path + "/tmp/" + req.getSession().getId() + ".html");
so the name of the file equals the session ID). Then I add an attribute to the session, whose value is the full path to the file. I proceed to populate my file as always, and when the user selects to log out, I invalidate the session. Then, inside the listener, I retrieve the path to the file, hence I can acquire the pointer to it:
String fname = ev.getSession().getAttribute("filename").toString();
File f = new File(fname);
f.delete();
So, now the messages I am getting are positive, I mean f.delete()
returns true, and after this I do f.exists()
and I get false
. So it should be OK. However, the files physically exist, that is they are still present on the disk.
I can try the example so kindly provided by @A4L. Have I done something wrong..?