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..?
Please stop writing arbitrary files to deploy folder during runtime. Just write to a real temp folder instead. Get rid of the following line altogether:
path = request.getSession().getServletContext().getRealPath("/");
and just use
File file = File.createTempFile("getter", ".html");
Your concrete problem is likely caused because the files in the deploy folder are usually locked by the servletcontainer. You can't delete files therein.
A hint for the future: whenever you occur to think that the getRealPath()
could solve a problem, you should immediately stop writing code and think twice about whether it's the right tool to fix the concrete problem. In the decade I developed Servlet based web applications, there was no one sensible real world use case for that method. See also What does servletcontext.getRealPath("/") mean and when should I use it
I know about file.deleteOnExit(), but... when do I exit a servlet?
You don't. The container does. The "exit" here basically means the shutdown of the whole JVM. This is even literally mentioned in the javadoc (emphasis mine).
Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With