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:

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

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:
#New property file
#Fri Jan 09 21:52:54 EST 2015
Name=JavaHonk.com
PHONE=123456789
Location=NY
  • LoadPropertiesFileServlet.java:
package com.javahonk;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoadPropertiesFileServlet
 */
@WebServlet(description = "LoadPropertiesFileServlet", urlPatterns = { "/LoadPropertiesFileServlet" })
public class LoadPropertiesFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoadPropertiesFileServlet() {
        super();
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Properties properties = new Properties();
		properties.load(getServletContext().getResourceAsStream("/resources/JavaHonk.properties"));
		Enumeration<?> enumeration =properties.propertyNames();
		while (enumeration.hasMoreElements()) {
			String key = (String) enumeration.nextElement();
			System.out.println("Key: "+key+"   Value: "+properties.getProperty(key));				
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
  • 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 *