views:

36

answers:

1

Hi, I am using jboss, eclipse and svn together. I have to files in my test folder: test/create.jsp and test/data.txt . What I want to do is when I call my create.jsp it will update my data.txt . Obviously I want my data.txt to stay where it is as other scripts are tryong to read from it.

I have tried dozens of new ways to put the path to my File object but for some reason it creates the file under jboss war folders.

Tried:

ServletContext app = getServletContext();
String path1 = app.getRealPath("/");
File f = new File(path1);
// AND
File f = new File("../../data.txt");
+2  A: 

Assuming that /test folder is located in the webcontent, then you need the following:

String absolutePath = getServletContext().getRealPath("/test/data.txt");
File file = new File(absolutePath);

or

String webcontentRoot = getServletContext().getRealPath("/");
File file = new File(webcontentRoot, "test/data.txt");

Do you see it? The Java IO only understands local disk file system paths, not URL's or paths outside the context. The ServletContext#getRealPath() is to be used to convert a relative web path to an absolute local disk file system path which in turn can be used further in the usual Java IO stuff. You should never use relative paths in Java IO stuff. You will be dependent on the current working directory which may differ per environment/situation.

That said, you normally don't want to write files to the webcontent. They will get lost whenever you redeploy the WAR. Rather create a fixed disk file system path somewhere else outside the webapp and make use of it. Or even better, make use of an independent SQL database :)

BalusC
Both absolutePath and webcontentRoot point to one location and its: "C:\workspace\infweb\gen\jboss\jboss-4.2.2.GA\server\default\.\deploy\infweb.war\test\data.txt" which I really want it to be "C:\workspace\infweb\src\web\prod\test.txt" May be I am just confusing the paths and not understanding fully on what happens when you deploy.lol and yes, if it was in my hand I would have used sql and my life would've been much easier.
Mazzi