Two options for showing properties from a .properties file
Task: Show in a jsp file some properties you have declared in a .properties file in the project
Solution1:
Use the Jquery Plugin for internationalization. Think of this as using the ResourseBundle of Java. Loading and parsing the information from the .properties files and showing them in the jsp page.
This is the official documentation of this plugin: jquery
To simplify this, here is what you need to:
- structure your resources: create a “scripts” (just an example of naming) folder in your WebContent folder. Here add the Jquery script(get the latest from here. ), the internationalization plugin(from here )
- and create another js file of your own in which you state that on page loading you want to invoke the plugin:
jQuery.i18n.properties( { name : 'messages', path : 'i18n/', mode : 'map', });
These code snippets can be found in the official documentation as well.
- create another folder, my choice of names was: i18n and here add your properties file: “messages.properties”. In this file you will add your properties, for instace:
ro.client.welcome_text = "Welcome message..."
- next, in your jsp file import the /js files we talked about above.
Now, in the jsp file, add id’s to the div’s or other html tags to where you want to print the properties from the .properties file. Example:
<b><label id = "welcome_text"></label>
Now, in the head tag of the page, in a javascript tag, on document ready get the above id and replace it with your wanted property:
$j('[id=welcome_text]').text(jQuery.i18n.prop('ro.client.welcome_text'));
And this is it. Pretty straight forward right?
Solution 2:
Same task, different approach.
Nothing of the above, nothing to import, in the body add this block:
<%@page import="java.io.*,java.util.*" %>
<% Properties prop=new Properties();
InputStream stream = application.getResourceAsStream("the path to your properties file");
prop.load(stream); %>
And when you want to show the properties:
<strong><% out.println(pro.getProperty("welcome_text")); %></strong>
And this is about it too.
Furthermore, I found this interesting, well written article about best practices regarding JSP( here ). Remember this “Note that you should not place sensitive data in JSP source code, not even inside JSP comments” and be with care when adding logic in your jsp page.


Tags: 



3 Responses
August 2, 2010 1
Give you free coffee gifts
August 25, 2010 2
excellent writing .
October 18, 2010 3
thanks for the post
Leave a Reply