Which path should I use when calling the java properties file?

Basically, I have a model of my project set up this way:

ModelFolder
----- CSI
----- bin, etc.
----- PropertiesFolder1
--------File1.properties
--------File2.properties, etc.
----- PropertiesFolder2
--------File1.properties
--------File2.properties, etc.
-----MainPropertiesFile1.properties
----- MainPropertiesFile2.properties

I try to use it with my view, which is a dynamic web project, and I got property files that finally loaded in my web project after changing

foo.load(new FileInputStream("foo.properties")); 

to

foo.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("foo.properties"));

and export the project to a JAR file, which is then included in WEB-INF / lib. However, I had to add another method to the model, and when I tried to test this method, the model was unable to read my properties file. I know that I can use FileInputStream with the full path to get a properties file that works in the model and view, but are there any alternatives?

I don’t want to constantly change the full path every time I switch computers (I use H: \ username \ ... \ Java \ Workspace at work, while at home it is just C: \ Java \ Workspace).
I also do not want my property files to be moved to different folders; and finally, I don’t want to change the way the properties file is loaded every time I test my model or my view.
Is there any way to do this?

It drives me crazy, I tried all of the following:

 try { foo.load(this.getClass().getResourceAsStream("foo.properties")); //foo.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("foo.properties")); //foo.getClass().getResourceAsStream("foo.properties"); //foo.load(new FileInputStream("foo.properties")); } catch (IOException ex) { al.logIntoProgrammerLog(ex); } 

All these lines either work in the model or in the view. Is it possible to somehow call these property files through the relative path in the model, and then somehow properly connect the model to the view so that all files are found and loaded?
Any help would be greatly appreciated; I am new to Java, so maybe something very simple to me. Thanks.

EDIT:
Sorry for not clarifying this, the model is a Java project, while the view is a dynamic web project running on the local Tomcat Server version 6.0.

Better (hopefully) explanation:

My View has a LoginServlet with the following doPost method:

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("usernameField"); String password = request.getParameter("passwordField"); ActivityLogger al = new ActivityLogger(); LoginController l_c = new LoginController(); //loginUser method will call my UserStorage class and //will return true if UserStorage finds the User with those credentials in the db //UserStorage is using a prepared sql statement stored in a properties file to find the User //That properties file is not being read unless I specify the full path to it. //Both Login and UserStorage are in the Model if(l_c.loginUser(username, password)) { //take user to welcome page } else //display error } 

Thanks again

+4
source share
2 answers

If you are in a web application, you should be able to get the path (the relative path in the webapp context for it to work on any computer) using ServletContext getResourceAsStream() . For example, if you want to get the path from your servlet method doGet() :

 public void doGet(HttpServletRequest request, HttpServletResponse response) { InputStream in = getServletContext().getResourceAsStream(<path>); } 
  • where <path> will be the relative path to your .properties file (i.e. / WEB-INF / foo.properties or wherever this properties file is located ... look in the deployment folder to find out for sure)

But in re-reading your message, it seems that perhaps your “Model” is not a webapp, but your “View” is a web application? Perhaps you could clarify whether these two different applications are - and perhaps one of them is a webapp running in a servlet container (i.e. Tomcat, Glassfish, etc.), and one of them is a standalone application ? If this is the case, then this is more of a problem with the shared file than the problem of "not finding the resource." A little clarification about the nature of your application (s) will help to send the correct answer ...

+1
source

Take a look at this thread for possible solutions for sharing the .properties file. But I would advise you to consider this approach to see if this is really what you want. More specifically, if your .properties file contains SQL queries, are you sure you need it in your view? It looks like you can make a clean split there.

+2
source

All Articles