Posts

Showing posts from January, 2009

Left shift(<<) and right shift(>>) operators

This is a quick answer for what a left shift( >) operators do in Java. A left shift operator returns a value that is obtained by multiplying the left hand operand with 2 raised to the power of right hand operand. eg. 5 > 2 = 5 / (2 raised to the power 2) = 2. There are several factors that decides the behaviour of the operation. One is the sign of the integer.

Difference between servlets and JSP

Many people think that difference between JSP and servlet is that a JSP cannot be mapped to some other URL and JSP's don't have a life cycle mechanism. That is not correct. It is possible to map any URL to a JSP file. The configuration is almost the same except that instead of element element is used. Also a JSP can define init and destroy methods. The JSP should implement jspInit() and jspDestroy() methods as jsp declarations. It is possible to specify element for a JSP file and load the JSP file on container start up. Then what is the difference between JSP and servlet ? Some differences I can point out are 1 , JSP can contain HTML mark up same as in a pure HTML file where as in a servlet it needs to be put inside Java code. HTML editors can work with JSP files. 2 , JSP has a scripting and expression language. 3 , A servlet has access to underlying ServletOutputStream and can send binary data to the client. A JSP gets a java.io.Writer object only and it is not suitabl...

Methods for generating random data for testing

Very often for testing purpose huge amounts of data is required. Instead of using dictionary words it is better to generate random words and numbers. Here is a class with methods that generate random random String data. import java.util.Random; public class RandomStringGeneratorUtil { private final static char[] alphabets = { 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' , 'u' , 'v' , 'w' , 'x' , 'y' , 'z' }; /** * Use this method to generate strings of specified length. * * @param rng * @param chars */ public static void randomString(Random rng, char[] chars) { for(int i=0; i chars[i] = alphabets...