Monday, November 16, 2009

Interesting finding on html form submit using java script

Consider the following simple html code

The above code displays a simple page with a text box a "submit" button and a "submit" link. Seeing the code one can understand that a form will be submitted whenever the click on the submit button or submit link.

PROBLEM:

Hold on, just try the above example if you click on the "submit" button your form will be submitted without any problem and tries to display "test2.html" however if you click on the "submit" link the form will not be submitted and it will remain in the same page. SURPRISING and WHY?

CAUSE:

document.forms[0].submit() method will not work if any of the form elements are named as "submit". Strange but true.

Solution:

Use document.forms[0].submit.click(); method to invoke the submit button click event which will have the form to be submitted.

Hope this is useful to you all because everyone uses the above type of code. It almost took me a day to identify the real cause pf the problem

Friday, July 3, 2009

Principles from my developer experience

1) Play with the technology, and not allow the techology to play with you

2) Make your team lead to guide you and not to drive you

3) Come with a solution to a problem not with a complaint towards a solution

4) Work to learn not to earn because earning will come automatically if you learn new things

5) Mould your attitude according to your team and not expect the team to mould their attitude according to you.

6) Concentrate on the way you work rather on the type of work you do to get work satisfaction

7) To learn from your role model, look into how they do rather than what they do

Tuesday, May 5, 2009

Encrypting Database passwords in JBoss Data Source File

If anybody uses JBoss as the application server, then they should be familiar with configuring JNDI data sources in JBoss. Generally to configure these JNDI based data sources they have to do the following:
  • Create a data source file under "deploy" folder with the name such as "oracle-ds.xml" or "hsqldb-ds.xml" etc.
  • In the above created file one can add the data source details as follows

<datasources>
<local-tx-datasource>
<jndi-name>ClusterSix</jndi-name>
<connection-url>jdbc:oracle:thin:@10.0.0.0:1521:SID</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>user</user-name>
<password>pwd</password>
<min-pool-size>2</min-pool-size>
<max-pool-size>5</max-pool-size>
<blocking-timeout-millis>60000</blocking-timeout-millis>
<idle-timeout-minutes>1
<valid-connection-checker-class-name>
org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker
</valid-connection-checker-class-name>
</local-tx-datasource>
</datasources>


This is all you need and you have a JNDI associated with your DB. The problem with the above approach is the password of the DB is exposed to the outside world and hence it may be s security issue. Do you agree with me?

It would be nice if there is a way to encrypt the password and use it safely in the data soure file. Does JBoss supports such type of encrypted passwords? The answer is "YES" it supports.

Below is the process which tells how to use encrypted passwords in your data source files.

The SecureIdentityLoginModule from jboss-jca.jar can be used to encrypt database passwords rather than using clear text passwords in the datasource configuration. It uses a hard-coded password to encrypt the datasource password. You can encrypt the datasource password using the SecureIdentityLoginModule main method by passing in the cleartext password


Step 1: Encrypt your password:

  • cd into the root of your jboss server. ex: cd /usr/local/jboss-3.2.7
  • execute java -cp lib/jboss-jmx.jar:lib/jboss-common.jar:server/default/deploy/jboss-jca.sar:server/default/lib/jbosssx.jar org.jboss.resource.security.SecureIdentityLoginModule
  • The result will be your encrypted password such as 5dfc52b51bd35553df8592078de921bc

Step 2: Add the encrypted password to the data source file (oracle-ds.xml)

<datasources>
<local-tx-datasource>
<jndi-name>ClusterSix </jndi-name>
<connection-url>jdbc:oracle:thin:@10.0.0.0:1521:SID</connection-url>

<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<security-domain>EncryptClusterSixPassword</security-domain>
<min-pool-size>2</min-pool-size>
<max-pool-size>5</max-pool-size>
<blocking-timeout-millis>60000</blocking-timeout-millis>
<idle-timeout-minutes>11</IDLE-TIMEOUT-MINUTES>
<valid-connection-checker-class-name> org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker
</valid-connection-checker-class-name>
</local-tx-datasource>
</datasources>

You can observe that the above Datasource configuration does not contain username and password tags as shown earlier, in place of these we could see a word "EncryptClusterSixPassword". Now where is this word came from and where did we mentioned the encrypted password generated earlier. From the third step you will get an idea what happened.

Step 3: Add an entry to login-config for each datasource:
Add the following for each datasource in login-config.xml. Make sure that the jboss.jca:name equals the data source defined in oracle-ds.xml. You will need one for each cluster.

<application-policy name="EncryptClusterSixPassword">
<authentication>
<login-module flag="required" code="org.jboss.resource.security.SecureIdentityLoginModule">
<module-option name="username">user1</MODULE-OPTION>
<module-option name="password">
-5dfc52b51bd35553df8592078de921bc
</MODULE-OPTION>
<module-option name="managedConnectionFactoryName">
jboss.jca:name=ClusterSix,service=LocalTxCM
</MODULE-OPTION>
</LOGIN-MODULE>
</authentication>
</APPLICATION-POLICY>


Note that login-config.xml file name should be as it is and it should be placed under /jboss/server/conf folder.

That is all, you are done and no password is mentioned anywhere except the encrypted one which you can use safely.

Hope this turtorial is helpful to you, please let your comments...

Wednesday, April 1, 2009

Problems with Math.Pow() method in java

If you have any business logic which goes with computing 2 power n where n can be any integer identifying a partiuclar value, don't user Math.pow(2,n) method since it takes decimal number and the maximum value of "n" for which it can compute the results correctly is n=63 so if for any "n" value greater than 63 it will round the number and gives wrong results . To overcome this problem choose java.lang.BigDecimal object to compute powers.

From java 1.5 the BigDecimal class supports the pow() method which can compute correctly for any large numbers. It also comes with add() method which allows you to add large number correctly.

Tuesday, March 31, 2009

Handling Browser back button Issue in Java Script

If you were a web application developer you might have encountered a problem: "How the hell do I need to handle browser back button?" To your surprise if you do google search you wont get any useful information which helps you really to solve your problem. I worked on this type of problem along with my colleague and found one solution to these type of problems which I am going to share in my blog.

First let us discuss about the problem. Consider you have two pages (page1 and page2) where we can navigate page2 from page1. Let us assume that page2 will display a web form which has to be filled by the user and submit it to the server. Suppose if you have a requirement such that you need to warn the user whenver he click the back button when he is in page2 so that he may not loose any data he filled in the form. How do you think you can handle this?

There is no Javascript even which triggers when you click on the back button which will let your code know that a back button event has happened. The solution for this is using window.onbeforeunload event. From IE 4.0 Microsoft has introduced this event which can be used to control the browser history, keep track on back button click and so on. This event can help you to handle two things in your web page:
  • It will allow you to display a decision dialog box before the user leaves the current page
  • It will cause the browser to never do caching of the page

Eg. you can code like this

window.onbeforeunload = function() {
return "All your unsaved data will be lost"
}

This will display a decision box like
Are you sure you want to navigate from this page?
All your unsaved data will be lost
Click OK to continue, or Cancel to stay on the current page

You can notice that whatever string you placed in your return statement that string is wrapped up with two additional statements as shown above. So whenever a user clicks a back button this event is triggered and displays the above message and if he clicks on "OK" the page will navigate to prev page or else it should remain the same. If you dont want any confirmation box to be displayed to the user, dont use any return statement.

Given you know the above event you can use that event to handle any type of business logic you want to happen before the user leave the current page.

Hope this will be useful to you.

For additional information you can visit http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript