GeeCON Prague 2014*
The moose likes Servlets and the fly likes deleting a file in the same context from servlet! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "deleting a file in the same context from servlet!" Watch "deleting a file in the same context from servlet!" New topic
Author

deleting a file in the same context from servlet!

Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
hi
i am trying to delete a file from my servlet in tomcat 4. the files are as
myapplication/myimages/1.gif.
My servlet location is
myapplication/web-inf/classes/pkg.delServlet
I have tried creating the file object like this in the delServlet
File file= new File("../../myimages/1.gif");
//-- And
File file= new File("../myimages/1.gif");
//--- And also with full path
File file= new File("C:/tomcat/webapps/myapplication/../myimages/1.gif");
In all the ways the file is not being deleted though i can delete it from another standalone class not running in the server with the same lines.
Can anyone help me what i m doing wrong here.
Thanks
Gul
Amit KumarS
Ranch Hand

Joined: Oct 10, 2003
Posts: 100
consider writing
File file= new File("../../../myimages/1.gif");
and let me know if it works
Thanks
Amit


****************************<br />In 24 hrs Earth rotates once on its Axis.
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
servlets are not executed where you deploy them, use this method
getServletContext().getRealPath("myimages/1.gif");
make sure your web application has permission to modify the files within these directories



Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
Originally posted by Amit KumarS:
consider writing
File file= new File("../../../myimages/1.gif");
and let me know if it works
Thanks
Amit

Thanks Amit but it didnt work either. It doesnt show any error or exception. just the files are still there.
Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
Originally posted by Tim Baker:
servlets are not executed where you deploy them, use this method
getServletContext().getRealPath("myimages/1.gif");
make sure your web application has permission to modify the files within these directories


Thanks for the reply.
I also have to perform this task from a plain class and i wouldnt have the servlet context available there, though the class is being called by a servlet. How to do that from the class?
How to check if i have the permission to delete? though the file and directory are within the webapplication context.
Regards
Gul
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
two options
+ hardcode the location properly
File file= new File("C:/tomcat/webapps/myapplication/../myimages/1.gif");
this was wrong because .. means 'up a directory' so i think what you probably meant was just
File file= new File("C:/tomcat/webapps/myapplication/myimages/1.gif");
+ pass the Context object to your class from the servlet so that the class can then use the getRealPath method
the second is the best because your servlet will be portable.
to check that your servlet has permission look in tomcat/conf at your catalina.policy files to make sure your context has an entry iving it write permissions, you can find more info about this on the web if you search for catalina.policy
Gul Khan
Ranch Hand

Joined: Sep 03, 2003
Posts: 173
Thanks for the reply. for now the getServletContext().getRealPath is working quite well and also portable. but still i will move it to my class according to the way u just told me
Thanks again and regards
Gul
 
GeeCON Prague 2014
 
subject: deleting a file in the same context from servlet!