Posts

Showing posts from July, 2008

Using Java Properties File

Many programmers use properties files for keeping configuration parameters (DB Server IP etc.) Here is a sample class code for accessing configuration parameters from a properties file. You can pass JVM parameter like this : java -DMyPropertiesFile=/usr/local/java/proj/myconfig.properties MyClass package javacream; import java.io.InputStream; import java.util.Properties; public class AppProperties { private static Properties prop; static { initiate(); } protected static void initiate() { try { //Pass Java VM following arguement // -DMyPropertiesFile="[PATH]/[PROPERTIESFILENAME]" prop = new Properties(); String propertiesFile = System.getProperty("MyPropertiesFile"); if (propertiesFile == null) { System.out .println("Could not find content.properties property in System Properties."); System.out .println("This property file is needed for the application to operate."); System.out .println("Se

Java MD5 Hash Sample

MD5 (Message Digest) is an encryption algorithm that creates a unique hash from a given data which is completely reverseble. This means you cannot recalculate original data using its hash , and hash is universally unique. Here is a sample code that produces MD5 hash of a given string : package javacream ; import java.security.MessageDigest ; import java.security.NoSuchAlgorithmException ; public class MD5HashTest {     private static String getMD5Digest ( String str ) {         try {             byte [ ] buffer = str. getBytes ( ) ;             byte [ ] result = null ;             StringBuffer buf = null ;             MessageDigest md5 = MessageDigest . getInstance ( "MD5" ) ;             //allocate room for the hash    result = new byte[md5.getDigestLength()];             //calculate hash    md5.reset();             md5. update ( buffer ) ;             result = md5. digest ( ) ;             System . out . println ( result ) ;             //c

Java HTPPS SSL Client

HTTPS secure HTTP connection , HTTP over secure socket layer (SSL). To establish an HTTPS connection to a remote server , you should use the certificate obtained by the server. This certificate is validated using a local trust store. Default trust store is /lib/security/cacerts. You can define another trust store using keytool application in /bin. Certificates can be added to trust store with keytool application also. You should give trust store parameter to JVM like this : -Djavax.net.ssl.trustStore=/mypath/my.keystore -Djavax.net.ssl.trustStorePassword=mypassword Here is the sample code to create an HTTPS connection: package javacream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.URLEncoder; import java.security.Security; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; public class HTTPSClient { public static void main(String[] args

Sample JSP Date Combo

This method produces required html code for date time combos. Date parameter is selected in case of equality. public static String printDateCombos(String name , java.util.Date date) { Calendar today = Calendar.getInstance(new Locale("en-US")) ; if (date == null) today.setTimeInMillis( System.currentTimeMillis() ); else today.setTime( date ) ; int year = today.get( Calendar.YEAR ); int month = today.get( Calendar.MONTH ); int day = today.get( Calendar.DAY_OF_MONTH ); int hour = today.get( Calendar.HOUR_OF_DAY ); int min = today.get( Calendar.MINUTE ); StringBuffer out = new StringBuffer(); out.append("<select name=\""); out.append(name); out.append("_day\">"); for (int i=1;i<32;i++) { out.append("<option"); if (i==day) out.append(" selected "); out.append(">"); if (i<10) out.append('0'); out.append(i); out.append("</option>