Load Properties File Servlet Java

Load Properties File Servlet Java

In this demo you will see how to load properties file using servlet and read its value. Basically we get confuse when we try to load properties using below method and get file not found exception:

Definitely if you use above logic to load property file in web project you will get file not found exception. This is because /WEB-INF/resources is not part of classpath. This will work only if you put resources folder inside /WEB-INF/classes folder which is part of classpath. Below you will see how to load properties file which is not part of classpath using servlet.

  • Sample servlet based dynamic project structure name: LoadPropertiesFileServlet

Load Properties File Servlet Java

  • We will read below properties file name: /resources/JavaHonk.properties file inside servlet get method and print all its value on console:

  • LoadPropertiesFileServlet.java:

  • For this demo we are running this project on tomcat server inside eclipse. If you are not sure how to configure and run tomcat in eclipse please use this tutorial. Use below URL to execute servlet:

Load Properties File Servlet Java

  • Output on console:

Load Properties File Servlet Java

  • For more information about servlet please refer oracle tutorial here

download Download Project:  LoadPropertiesFileServlet

Leave a Reply

Your email address will not be published. Required fields are marked *