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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete files 1

Status
Not open for further replies.

ramkumars

Programmer
Aug 2, 2001
31
IN
Hi, i have a page which creates a file and makes it downloadable to users. but when the user leaves the page, i need to delete these files. can anybody tell me how i can do this?
 
I doubt this is possible at all using JSP. Perhaps you can consider workarounds instead of spending time on finding a solution for this...
 
Hi LeonTang,
i was wondering if this can be done at the end of the session(not necessarily when the user leaves the page).is there anything similar to session_onend routine which is available in ASP?
 
Create a Session Listener (assuming you are using Servlet 2.3). You need to implement the interface
Code:
javax.servlet.http.HttpSessionListener
. There are two methods:
Code:
public void sessionCreated(HttpSessionEvent se)
public void sessionDestroyed(HttpSessionEvent se)
Obviously you will be more interested in sessionDestroyed(). After writing and compiling your class, you need to edit the web.xml file for your web application and add the listener. That's it, pretty easy. Wushutwist
 
Here is an example.
SessionCleanUp.java:
Code:
package com.mypackage;
import javax.servlet.http.*;
import java.io.*;

public class SessionCleanUp implements HttpSessionListener {
  public void sessionCreated(HttpSessionEvent se) {
    /* Do any session initialization */
  }
  public void sessionDestroyed(HttpSessionEvent se) {
    /* Clean-up Session */
    HttpSession session = se.getSession();
    File userFile = session.getAttribute("userFile");
    if (userFile != null) {
      try {
        userFile.delete();
      }
      catch (Exception e) {
        /* Do any error-handling */
      }    
    }
  }
}
web.xml:
Code:
<listener>
  <listener-class>com.mypackage.SessionCleanUp</listener-class>
</listener>
I didn't test the code but I have no reason to think that it won't work. Please note, for some reason this forum keeps dropping the uppercase H's in my code examples. Anywhere you see httpSomeClass, the h should be capitalized. Wushutwist
 
can you tell me where i should place the SessionCleanUp.java
(or class) file?
 
Hi Wushutwist, sorry for my ignorance but I thought that since the file is &quot;downloadable&quot;, doesn't it means that the file is transfered and stored on the user's pc instead of in the session (according to your example)? And if so, doesn't it make the program &quot;unworkable&quot; for this case? Hope you can enlighten me on this one :)

Regards,
Leon
 
If ramkumars meant what you are saying Leon, than you are absolutely correct. I took it as meaning, &quot;I am creating a file on the server that is available for download by a user. After the user is gone, how do I delete this file from my server.&quot;

In my example I was assuming that a File object (which isn't an actual file in a memory sense) is being stored in the Session. This File could be used to access the actual OS file.
Wushutwist
 
icic... I was thinking how can JSP (which is actually servlets) delete files when they are residing on the server...hehe... anyway thanks for resolving my question.

Leon
 
Hi LeonTang and wushutwist,

i found your conversation interesting. anyway the problem was such that, the file which was downloadable resides(or is created) in a particular directory in the server and when the user leaves the site, the files in this particular directory was supposed to be deleted. i was able to achieve this by using a servlet (is it a servlet by the way?) similar to SessionCleanUp.java (which wushutwist provided). But this works with only Tomcat4(which has support for servlet 2.3) and not in Tomcat 3.2
 
Looks like I have misread your question, but glad that you have found your answer. JSP are compiled into servlets during run time so normally the first time you access them, it will be slow and the subsequent time, faster.

Leon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top