2

I am using tomcat server for my java application(Servlet,jsp).In my servlet page calling one java class function.Actually the java file is written separately and i will call the function written inside it.With in the function, i have to read one config(user defined) file for some purpose.so, i am using File class for that.

But, here i have to give relative path of the config file(user defined).Because, now i am running this application in local windows server.But my live server is based on Linux.So, the file path is changed in linux.

File f1=new File("D:\tomcat\webapp\myapp\WEB-INF\src\point_config.txt");  -- windows
File f1=new File("D:\ravi\tomcat\webapp\myapp\WEB-INF\src\point_config.txt");  -- linux

So, i have to give relative path of the file that is common to both windows and linux machine.

Is there a way to do this?

Please guide me to get out of this issue?

Saravanan
  • 11,372
  • 43
  • 143
  • 213

2 Answers2

6

Place your config file under your webapp WEB-INF/classes folder and read like this in code

InputStream is= 
   YourClassName.class.getResourceAsStream("point_config.txt");
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

The path of the config file leads into the WEB-INF folder

tomcat\webapp\myapp\WEB-INF\src\point_config.txt

Anything inside WEB-INF is protected and cannot be user-defined once the web application has launched. If you meant to read from a user-defined configuration file from the file system, please use an API like the common configuration API.

If you want to insist on keeping the file inside the WEB-INF folder, use the Class.getResourceAsStream() method to obtain the configuration instead. That would not make the configuration user-defined though.

Deepak Bala
  • 11,095
  • 2
  • 38
  • 49
  • Are you sure the application can't reach inside WEB-INF? Can you provide a source for the statement regarding protection? I know a user can't hit a file inside WEB-INF, but I believe the application can. – Kevin Bowersox May 16 '13 at 09:40
  • 1
    @KevinBowersox No, the application CAN reach inside WEB-INF through `Class.getResourceAsStream()`. The user cannot reach inside WEB-INF. I mentioned that to stress that a property inside WEB-INF is not really user-defined. – Deepak Bala May 16 '13 at 09:47