Upload Property File to Servlet / JSP

I created a jar from my Java project and wanted to use the same jar in the JSP Servlet Project . I am trying to load a let say sample.properties property file from my JSP Servlet Project stored in WEB/properties/sample.properties , which should be read by the class in jar . I am using the following wriiten code in the jar class to access it.

 Properties prop=new Properties(); prop.load(/WEB-INF/properties/sample.properties); 

But every time I get a fileNotFound exception .
Please offer me a solution.

Here is the structure

 WEB-INF | lib | myproject.jar | myclass (This class needs to read sample.properties) | properties |sample.properties 
+5
source share
5 answers

The /WEB-INF folder is not part of the class path. That way, any answer that thoughtless suggests ClassLoader#getResourceAsStream() will never work. It will only work if the properties file is placed in /WEB-INF/classes , which is really part of the class path (in an IDE such as Eclipse, just placing it in the root folder of the Java source code should be sufficient).

If the properties file is really where you want to save it, you should get it as the web content resource ServletContext#getResourceAsStream() instead.

Assuming you are inside an HttpServlet , this should do:

 properties.load(getServletContext().getResourceAsStream("/WEB-INF/properties/sample.properties")); 

( getServletContext() inherited from the servlet superclass, you do not need to implement it yourself, so the code is as-is)

But if the class itself is not an HttpServlet at all, then you really need to move the properties file to the class path.

See also:

+21
source

Try putting sample.properties in the src folder and then

 Properties prop = new Properties(); prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("myprop.properties")); 
+5
source

Move your property files under WEB-INF/classes . Then download it as follows:

prop.load(getClass().getResourceAsStream("sample.properties"));

You can also put it in a subdirectory in classes . In this case, change the call to getResourceAsStream() .

To be more secure on a multi-user system, you can use Thread.getContextClassLoader().getResourceAsStream() instead.

For the properties file to arrive in the classes folder of your war file, you must put it in the resources folder in your project (if you use maven) or only in the src folder if you do not use a structure structure like maven.

+2
source

Try it,

  InputStream inStream = Thread.currentThread().getContextClassLoader() .getResourceAsStream("/WEB-INF/properties/sample.properties"); 

Then load (InputStream) into the Properties object:

 Properties props = new Properties(); props.load(inStream); 
+1
source

It may not work. If you are trying to load properties from jsp / servlet. Write a utility class to read the properties and package along with the jar file. copy the properties file to the same package as the utility.

  Class Utility{ Properties properties=null; public void load() throws IOException{ properties.load(getClass().getResourceAsStream("sample.properties")); } public Object get(String key) throws IOException{ if (properties==null){ load(); } return properties.get(key); } } 

Now use this utility class from the servlet to read property values. Maybe you can define a class as singleton for best practice.

Greetings Satheesh

0
source

All Articles