Pages

Tuesday, 17 January 2012

sendRedirect vs RequestDispatcher

As you may already know that sendRedirect method in response object and RequestDispatcher object both are used to  redirect to another page in servlets,but both works in a different fashion.
RequestDispatcher forwards the request,response objects to another page/servlet this happens at the server side.
where as sendRedirect method makes a new request from client side but client is unaware of the process.

for example if you say in real time
if X is asking about some information to Y
if Y is confirming with Z and telling you the answer is RequestDispatcher mechanism.
or if Y is telling you that ask Z for that information then that is sendRedirect we can say(but client i.e X is unaware of the process since it is a automated flow of process that hapeens).

By using RequestDispatcher the url in the client browser will be the first page address(i.e Y in example),
in case of sendRedirect it changes to the second page address(i.e Z).

Monday, 9 January 2012

Null Pointer Exception

The most commonly seen exception(in java) for everyone is ofcourse nullpointerexception. In this post we will see how this eception occurs and how to get rid of it.

               As you might know java is a object oriented language if we want to call any method that has to be done by using object reference. So if at all a call to a method which is refering to null will throw a null pointer exception
           for example
     ArrayList list=null;
    list.add("hai");
 will throw null pointer exception so when ever you get such kind of exception just go to the line check the object which is calling the method is not refering to null.

Saturday, 7 January 2012

Java toString method


java's toString method is useful for giving string description about a  class so it's better to override tostring method for every class
for example
Class A{
public String toString(){

return "sample class written by iTfinGer";
}
public static void main(String[] s){
A a=new A();
System.out.println(a);
}


}

In the above program it will print the String returned by toString method not the hashcode of the object
So the output for the above program is

sample class written by iTfinGer

-------------------------
Class A{

public static void main(String[] s){
A a=new A();
System.out.println(a);
}

}


but the above code prints the  Hashcode.