Friday, June 29, 2012

JSTL : How to use fmt:message and ResourceBundle

There are two ways you can access values from a property file. Mostly a text value which can change in future are kept in property file and access it using fmt:message and ResourceBundle in JSP and JAVA respectively so that if the text values are changed in future it wont affect our code. We just need to change in property file and rest all will be taken care implicitly.

Lets see how to use fmt:message and ResourceBundle in JSP and JAVA respectively.

1. In JSP

If you need to access the property value in JSP, first lets create a property file lets say prop.properties. Add prop.properties in src folder of your application.

prop.properties

key1=This is value1
key2=This is value2

Add the mapping in web.xml or Spring's dispatcher-servlet.xml as shown below

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="prop" />
</bean>

Note the value="prop" it says to load prop.properties from the src folder. Now through JSP you can access the property by its key as shown below

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<fmt:bundle basename="prop">
<fmt:message key="key1"/>
<fmt:message key="key2"/>
</fmt:bundle>

2. In Java

Also you may need to access these properties in a JAVA program it could be either bean or any controller. You can access using java.util.ResourceBundle as shown below.

String value1 = ResourceBundle.getBundle("prop").getString("key1");
String value2 = ResourceBundle.getBundle("prop").getString("key2");

Note : You may get below error

org.apache.jasper.JasperException: The absolute uri: http://java.sun.com/jsp/jstl/fmt cannot be resolved in either web.xml or the jar files deployed with this application

To solve above error you need to include jstl-1.2.jar in your application's classpath. It could be possible that your application is not able to detect jstl-1.2.jar file.

Javascript : Encode special characters

In Javascript, you may required to escape special characters from a text at some point of time and later you may need to retrieve the original text. This may be because of some logic or for passing as argument in some function. The basic is very simple we will write a encode function which will encode the special characters and a decode function which will get the original text out of encoded text.

Please use the below functions in your code accordingly.

<script language="Javascript">


   var toEncode = new Array(" ","!","@","#","$","%","^","&","*","(",")","-","_","=","+",":","\;",".","\"","'","\\","/","?","<",">","~","[","]","{","}","`");
 var toDecode = new Array("%20","%21","@","%23","%24","%25","%5E","%26","*","%28","%29","-","_","%3D","+","%3A","%3B",".","%22","%27","%5C","/ /","%3F","%3C","%3E","%7E","%5B","%5D","%7B","%7D","%60");

 function encode(val)
 {
  for (var i = 0; i < toEncode.length; i++)
  {
   val = val.replace(toEncode[i],toDecode[i]);
  }
  
  return val;
 }

 function decode(val)
 {
  for (var i = toDecode.length; i >= 0; i--)
  {
   val = val.replace(toDecode[i],toEncode[i]);
  }
  
  return val;
 }

</script>

Monday, June 18, 2012

DWR Hello World example

DWR - Direct Web Remoting

DWR is a Java library that enables Java on the server and JavaScript in a browser to interact and call each other as simply as possible. In simple words DWR is Easy Ajax for Java. Unlike AJAX you don't have to write codes to get XHTML object or check for state change etc. To know more about DWR, click here.

DWR is supported by almost all browsers like Firefox,Internet Explorer,Opera and Safari. To know more, click here.

About the example

We will create a simple java class (i.e HelloWorld.java) with a method sayHello() which will accept a name and print Hello name. We can call this java fuction with JavaScript in a browser. Same as Ajax call without refreshing the whole page.

Below are the steps to run the example.

1. For running the below example you need to add below mentioned JAR file in lib folder of your web project.

  • dwr.jar
  • commons-logging-1.1.1.jar 

For downloading dwr.jar, click here. 
commons-logging-1.1.1.jar, click here.


2. Add the DWR servlet definition and mapping to your application's web.xml. This is just like defining a normal servlet.


<servlet>
  <display-name>DWR Servlet</display-name>
  <servlet-name>dwr-invoker</servlet-name> 
  <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
  <init-param>
     <param-name>debug</param-name>
     <param-value>true</param-value>
  </init-param>
</servlet>
 
<servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

Monday, May 21, 2012

iCal4j example - Set a Meeting using iCalendar

iCalendar is a computer file format which allows Internet users to send meeting requests and tasks to other Internet users, via email, or sharing files with an extension of .ics. Recipients of the iCalendar data file (with supporting software, such as an email client or calendar application) can respond to the sender easily or counter propose another meeting date/time.

iCalendar is used and supported by a large number of products, including Google Calendar, Apple iCal,IBM Lotus Notes,Yahoo Calendar, Microsoft Outlook etc

iCal4j is an API which is used to modifying existing iCalendar data or creating new iCalendar data. To know more about iCal4j, click here.

About the example

This example is straight forward, we will create a calendar (net.fortuna.ical4j.model.Calendar) object and add a Meeting event to it. You can set various properties to the event like subject, location, description etc. Obviously you can customize it according to your requirement. This example will generate a .ics file (TestCalendar.ics). You can send this generated file via email to interested parties. Please see the below self explanatory java program.

For running below example you will need to add below mentioned JAR files in your classpath.
  • ical4j-1.0.3.jar
  • backport-util-concurrent-3.1.jar
  • commons-logging-1.1.1.jar
  • commons-lang-2.6.jar

To download the above JAR file, click here.

Friday, May 18, 2012

Text Extraction with Java


Documents in PDF format, are widely used and accepted within the computer world. And while it is a versatile piece of software, it does have its downsides. The most obvious being the inability to alter the document natively, therefore, text extraction is often sought after to try and make this tedious task less of an uphill struggle.

Extracting all of the useful, interesting, and relevant information from a PDF can be a daunting task, but by utilising text extraction software which is completely written in Java. The process of extraction and conversion has become increasingly crucial for the manipulation and archiving of data. With the use of a Java application, this has become extremely easy in regards to extracting text from a PDF document.