Wednesday 27 July 2011

Java-Encryption



When your application has log in form or registration form,you may need to save the passwords of the users to the db.The best practice when saving passwords is using java encryption.There are some implicit algorithms used for encryption and the following code uses the SHA algorithm.



So,now the encrypted values can be put to the db and the same can be used while validating the user entered password  with out any decryption.I mean follow the same encryption process and validate the entry.

More info on Cryptographic Hash functions can be found here.

Cheers..... :-)

Monday 25 July 2011

Jquery-Ajax



It is important to mention the return data type of response you are expecting from your ajax call.My response was simple string with delimiter as ^ and was working fine on I.E and Chrome.But,when switched to firefox, encountered some problem.The problem was I.E browser was  assuming return data type as text , and so it was fine,but where as the firefox is taking default type of response as  xml and was causing the problem.










So, please be sure about the data types supported and is good to mention in your ajax call to save your time.


Click here for more Info on data types supported
















 Cheers...... :-)

Tuesday 19 July 2011

Bubble Sort - Java







 Cheers...... :-)

Jquery -simplified js

In this article,let us see how we can use jquery in the place of  java script and minimize the lines of code.

Case 1:

let us say,your form has 100 input text fields and you have a use case clearing the values on click of reset button .This can be easily acheived using jquery as below where :input is the selector to select all input types









Case 2:

let's say you need the value selected in the group of  radio buttons ,let us consider the radio buttons name is gender














Case 3:


Lets take a use case where you have a number of check boxes which may need to be check or un-check at a single shot,this can be easily achieved as below















All the above stuff can be achieved using js but not that easily as in jquery.for more selectors refer JQUERY SELECTORS



cheers..... :-)

Sunday 17 July 2011

Jquery - charts



There are lot of jquery plugins and sophisticated js available for creating graphical reports.The following code uses the jgcharts.js to create the sample charts.

I'm using a plain html here and lil bit of Json for the data and remaining is self explanatory .






Cheers.... :-)

Singleton -the 1.5 way



Reference : Effective Java Edition 2


There are two ways to implement Singleton pattern in Java
1.  public final instance field or static factory method to create/get instance.
2. if you are using Java >= 1.5. You can use Enum for Singleton– this way you get Singleton functionality easily and you don't need to worry about the Serialization.

Sample Code:

  and can use in the following way.

 static DatabaseManager dbMgr=DBUtil.INSTANCE.getDBManager();


cheers....  :-)

Tuesday 12 July 2011

SEJ (Spring Ejp JSP)

Hi,

I started an application using the following.


Spring 3.0 (few concepts like DI,auto wiring,annotation based controllers)
EJP(for db operations)
JSP(front end),
Plain Ajax 
Jquery and JS
Mysql(backend)
Junit
Ant

Will keep posted all the details once done...


cheers... ;-)

Monday 11 July 2011

ConcurrentModificationException - Fail-fast Operations


Most of the time we use ArrayList ,add objects,remove and iterate through them in the List.Go through the following code.



The above code will result in 
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at CSSCreator.main(CSSCreator.java:16)

This is because of fail-fast operation .Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.


The best way to avoid ConcurrentModificationException is to use iterator while you have modifications to be done for the arraylist which you are iterating.

Using iterator in the following way also ends up with ConcurrentModificationException.


The right way of using it will be as follows and it makes sense because the iterator object knows where it is in the list as the list is being scanned.

 I had some tough time with this ConcurrentModificationException and thought of sharing(may be useful to some one at some point of time).....  

cheers ;-)

Saturday 9 July 2011

Spring 3.0 -Sample Controller Example


The following is the very basic Example Which I  tried out on Spring 3.0.The following screen hits include

1.Jars used for my sample.
2.Web.xml
3.Dispatcher-servlet.xml
4.Sample Controller code.
5.jsp samples.
6.Build.xml
7.Build.properties.

Since this code is of very basic,there's no much explanation deserved ;-)

1.Jars:






















2.web.xml









4.Sample controller:



































5. index.htm and result.jsps



<html>
<head>
<title>Spring 3 Example index.dp</title>
</head>
<body>
<a href="demo.html">Login</a>
</body>
</html>





<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>result</title>
</head>
<body>
${message}
</body>
</html>




Build.xml
 Tasks  deploy and deploywar in the following build file would use the build.properties to copy the war or the application folder to the webapps folder of tomcat. The lib and home mentioned in the build properties file may vary with tomcat versions and should change as needed.






build.properties :