views:

172

answers:

2

I have the following folder structure

TempProj
!-js
!-jsp
!-WEB-INF
  !-classes
  !-lib

Inside my lib folder, I have a java file that creates an XML file, I actually need the file to be generated inside the jsp folder.

I'm creating using StreamResult result = new StreamResult("test.xml");

I've tried giving the following paths "../../jsp/text.xml". Since I've deployed it, the path has to be relative.

Any help!!

A: 

What App Server you using ? If you are using Websphere (possibly others, but I don't know) , you might have to set up a 'file url' and use JNDI to delegate the job of finding the directory to the App Server itself.

As an experiment to shed more light on the problem, you could try something like:

File nopath = new File("text.xml");
System.out.println(nopath.getCanonicalPath());

And:

File relpath = new File("../../jsp/text.xml");
System.out.println(relpath.getCanonicalPath());

I wonder if you are going to be able to do this...I have a feeling (and that's all it is , please check!) that App Server's Security manager might not like you trying to traverse up and out of the directory....

You might want to reconsider not creating a file-on-disk - instead create a new servlet that dynamically serves up the XML...(presuming that you need the client-browser to see some XML ultimately...).

monojohnny
I'm using Tomcat
Panther24
Bozho, the second one worked perfectly. Thanks
Panther24
It wasn't actually me. I just edited a technical mistake in the second variant. But as the post is incorrectly marked as "community wiki", the original author does not appear.
Bozho
I set this to community wiki 'cos I thought this would be the best way of allowing edits...I may need to consult the stackoverflow help on this though :-)Glad you got this working.
monojohnny
Non-CW posts are also editable by others with enough reps.
BalusC
+1  A: 

You should never rely on relative paths that way. Using relative paths in Java IO is asking for portability trouble. The actual path would be dependent on the current working directory, which is not per se the same in all environments and/or the way how you started the server. Always use absolute paths, i.e. use the full disk file system path from the root on.

In case of a JSP/Servlet webapplication you can use ServletContext#getRealPath() to convert a relative web path to an absolute disk file system path which you can use further in Java IO stuff. A relative web path is rooted in the public webcontent. Your folder structure is odd and non-standard, but let's assume that you (re)named the webcontent as JSP, then you can get an absolute disk file system path for /jsp/test.xml inside a servlet the following way:

String relativeWebPath = "/jsp/test.xml";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
File xmlFile = new File(absoluteDiskPath);
BalusC
Agreed. Also there is another point here: you want to avoid writing stuff to the JSP directory - this (perhaps through a coding-error-created-backdoor) could lead to a security problem - where somebody could have your servlet drop a potentially executable file into your JSP directory and then subsequently run it.
monojohnny
Actually BalusC is a standalone program that has no access to the ServletContext
Panther24
Then just pass the `ServletContext` or the absolute `File` in as constructor or method argument.
BalusC