• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

deleting a file in the same context from servlet!

 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
consider writing
File file= new File("../../../myimages/1.gif");
and let me know if it works
Thanks
Amit
 
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Gul Khan
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 541
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic