Last updated on May 6th, 2022 at 12:57 pm

Read a property file using JSP and display it on a browser. This will be very handy for developers who need a quick and easy fix for their long search on the internet on how to load values from a property file using JSP and cannot find even a single working solution. We have used two methods to read the file one is using FileInputStream() and the other is using getResourceAsStream()

Lets begin this by creating a simple property file and name it as server.properties.

Below is the content of data.properties

DatabaseName=User_Base
SOURCE=MySQL

Now create a file with the name load.jsp and add the code as show below

Loading property file using getResourceAsStream()

<%@page import="java.util.*" %>
<%
Properties props=new Properties();
props.load(getServletContext().getResourceAsStream("/WEB-INF/data.properties"));
String appname=props.getProperty("DatabaseName");
out.println(appname);
String source = props.getProperty("SOURCE");
out.println("<br>"+source);
%>

Loading property file using FileInputStream()

<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%
Properties props=new Properties();
FileInputStream in=new FileInputStream("/usr/jboss/server/default/deploy/TestApp.war/WEB-INF/data.properties");
props.load(in);
String appname=props.getProperty("DatabaseName");
out.println(appname);
String source = props.getProperty("SOURCE");
out.println("<br>"+source);
%>

For FileInputStream() , we need to provide the absolute path instead of the relative path that was used for getResourceAsStream() . Hope this helped you in fixing some issues. Please like us on facebook and support us more.

Check out our tutorial on how to display servlet within JSP

Leave a Reply

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