5

I tried to read a file with my "RemoteServiceServlet" but the thing is

  • I want to create a dir which path is related to servlet like

absolute_servlet_path/myPackage/

But the problem is...

code like

ServletContext servletContext = this.getServletContext();
String pathContext = servletContext.getRealPath("");

... gives not "C:/..." but "/" only so the code cannot be used with java.io.File object.

So my question is how can I use java.io.File with RemoteServiceServlet ?

user592704
  • 3,674
  • 11
  • 70
  • 107

2 Answers2

3

OK... I had to keep digging...

Still I was looking for a way of servlet relative path but an absolute one; and now again I tried to use the context but this time I modified my code in this direction...

ServletContext servletContext = this.getServletContext();
String pathContext = servletContext.getRealPath("/WEB-INF/");

... and it worked for my Tomcat :)

I hope it saves one's day

Thanks

user592704
  • 3,674
  • 11
  • 70
  • 107
  • I hope you understand that this directory is where your WAR file is deployed. When you redeploy your application anything you put in there will most likely be erased. – Strelok Nov 06 '11 at 10:57
  • Sure :) I needed it just for my web app temp files only. I wasn't use user.home etc but to have a related path. So it is OK as for me :) – user592704 Nov 07 '11 at 17:08
  • 2
    A little unconventional. Perhaps you want to have a look at more standard ways to create temp files, discussed here http://stackoverflow.com/questions/617414/create-a-temporary-directory-in-java – Strelok Nov 07 '11 at 21:24
  • +1 @Strelok for your additional commenting me. Thank you but I really found the thing I wanted for my test app :) – user592704 Nov 08 '11 at 16:31
1

I think you misunderstand how servlets work. A "servlet" is just a class configured via the web.xml file to process requests on a given path at a URL. It's in now way related to the file system at all.

Why would you want to create a folder relative to your servlet's URL? What are you trying to accomplish?

Strelok
  • 50,229
  • 9
  • 102
  • 115
  • I am just trying to get the same effect as desktop app does. It is the default path like "." as for the File object. So not to use user.home or something :) – user592704 Nov 06 '11 at 01:07