Sample Application: Resource File

 

We'll start with index.jsp, the entry point of our application. This is the file directly accessed by the web browser.

This page demonstrates the usage of "properties" files in JSP. Initially the content of the file should be made to InputStream of bytes. This stream is loaded to Properties as keys and its corresponding values. Values can be retrieved by passing the corresponding key.

http://yourdomain.com/constants/index.jsp

 
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ page import="java.util.Properties, java.io.*" %>
<%--
This page demonstrates the usage of "properties" files in JSP. Initially the content 
of the file should be made to InputStream of bytes. This stream is loaded to Properties as 
keys and its corresponding values. Values can be retrieved by passing the corresponding
key.
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
 <%  
       //Read the input bytes from the file to a stream
       //Take the file path corresponding to context
       //If the file is inside a package give the full package name, 
       //For Eg: com/myApp/ApplicationResource.properties
       FileInputStream fin = new FileInputStream
       (getServletContext().getRealPath("ApplicationResource.properties"));
       
       //Represents a persistent set of properties.
       Properties p = new Properties();
       
       //Reads a property list as key-element pairs from the input stream
       p.load(fin);
 %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <!-- Reading value for the key "welcome.title" -->
        <title> <%=p.getProperty("welcome.title")%></title>
    </head>
    <body bgcolor="#ABBCD1" >
        <table align="center">
            <tr><td align="center">
                 <!-- Reading value for the key "welcome.heading" -->
                <h1><%=p.getProperty("welcome.heading")%></h1>
            </td></tr>
            
            <!-- Reading value for the key "welcome.content" -->
            <tr><td align="center">
                <b><%=p.getProperty("welcome.content")%></b>
            </td></tr>
        </table>
     </body>
</html>

 

index.jsp reads constans from the file and displays it.

[Page 1] [Page 2] [Page 3]

 
 
Contact Us