views:

37

answers:

2

I am making a web application in Eclipse (JSP) and use Tomcat as a server (integrated into Eclipse). I have to create the object below and specify the path to configuration file. This absolute path is working great: Store store = StoreFactory.create("file:///C:/Users/Aliens/workspace/myProject/WebContent/config/sdb.ttl");

However I am wondering why cant I use relative path? It should be "config/sdb.ttl" right (if the name of the project is a root)? But it cannot locate it this way (NotFoundException).

+1  A: 

Relative disk file system paths are relative to the current working directory which is dependent on how you started the application (in Eclipse it would be the project folder, in Command console it would be the currently opened folder, in Tomcat manager/service it would be the Tomacat/bin folder, etc). You have no control over this from inside the Java code, so forget about it.

In JSP/Servlet you can use ServletContext#getRealPath() to convert a relative web content path (it has its root in the public webcontent, in your case the /WebContent folder) to an absolute disk file system path. So:

String relativeWebPath = "/config/sdb.ttl";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
Store store = StoreFactory.create(absoluteDiskPath);
// ...

The ServletContext is available in servlets by the inherited getServletContext() method.

BalusC
I am creating this Store object in a class that is located in src folder. Since this is not JSP/Servlet I cannot use the method getServletContext(). In JSP I instantiate this class and in its constructor Store object is generated. Any other solution?
Aliens
A JSP file is semantically the [wrong place](http://stackoverflow.com/questions/3177733/howto-avoid-java-code-in-jsp-files) for the job. Regardless, since JSP files are compiled to a servlet, it has access to the `ServletContext` by `getServletContext()` as well. You can also use the implicit variable `application` which refers the same. I.e. `application.getRealPath(relativeWebPath)`. By the way, did you **actually try** it before saying "I cannot use the method"?
BalusC
Hey. Of course I tried it and it is still not working. I do not know if you understand me. I am not calling getServletContext() in a JSP. I want to call it in .java file in /src but I cannot do that. This .java file is a class. And that class is instantiated in JSP.
Aliens
I solved the problem. In JSP I get the right path following your advice. I am passing absoluteDiskPath as a parameter to my java class where I then use it to determine the path.Thanks for helping. BTW your article "how to avoid java code in jsp files" is very good.
Aliens
Oh, you said you were doing this in a JSP? Well, depending on the functional requirement, a `Filter` (request based) or a `HttpSessionListener` (session based) or a `ServletContextListener` (webapp based) is more suitable for the job. Regardless, you're welcome :)
BalusC
A: 

Right/standard/compatible way is to use http://adderpit.com/jdk/j2eedocs/api/javax/servlet/ServletContext.html#getResourceAsStream(java.lang.String)

like

servletContext.getResourceAsStream("config/sdb.ttl");
iimuhin