What is preinitialization of a java servlet?

In the java servlet life cycle, the first phase is called ‘Creation and intialization’.

The java servlet container first creates the servlet instance and then executes the init() method. This initialization can be done in three ways. The default way is that, the java servlet is initialized when the servlet is called for the first time. This type of servlet initialization is called lazy loading.

The other way is through the <load-on-startup>non-zero-integer</load-on-startup> tag using the deployment descriptor web.xml. This makes the java servlet to be loaded and initialized when the server starts. This process of loading a java servlet before receiving any request is called preloading or preinitialization of a servlet.

Servlet are loaded in the order of number(non-zero-integer) specified. That is, lower(example: 1) the load-on-startup value is loaded first and then servlet with higher values are loaded.

Example usage:
<servlet>
       <servlet-name>Servlet-URL</servlet-name>
       <servlet-class>com.javapapers.Servlet-Class</servlet-class>
       <load-on-startup>2</load-on-startup>
</servlet>

Can we make hashMap synchronized?

Yes, we can make HashMap to be synchronized.
Map hashMap = new HashMap();
Map m = Collections.synchronizedMap(hashMap);

Types of Inner Class / Nested Class in Java

 There are four type of inner classes in java. There briefly,
  1. Static inner class
  2. Non static inner class
  3. Local inner class
  4. Anonymous inner class
The class which is declared with inside class is called nested class / inner class. This is classified to two types. There are, Static Nested Class & Non Static Nested Class / Inner Class.
1. Static Nested Class:
  • Nested class which is declared static is static nested class.
  • Its methods & members associates with outer class.
  • It can be used through object reference.
  •  Example:
    OuterClass.StaticNestedClass innerStatic = new OuterClass.StaticNestedClass();  
               innerStatic.display(); 
Example for Static Nested Class
 class OuterClass{  
      // Declare static nested inner classs  
      static class StaticNestedClass{  
           //static inner class method  
           void display(){  
                System.out.println("Inside Static Nested Class");  
           }  
      }  
      //outer class method  
      void display(){  
           System.out.println("Outer Class");  
      }  
 }  
 class MainClass  
 {  
      public static void main(String[] arg)  
      {  
           //how to access static nested class method  
           //1st create object for outer class object  
           OuterClass outer = new OuterClass();  
           outer.display();  
           //Now calling static nested class   
           OuterClass.StaticNestedClass innerStatic = new OuterClass.StaticNestedClass();  
           innerStatic.display();  
      }  
 }  

2. Non Static Nested Class / Inner Class:
  • It has access to the other members of the outer class, even they are private.
  • As the static declaration are not allowed inside inner class, we can't run inner class directly console.
  • We can access inner class code through outer class object only.
  • Instance of inner class valid within the enclosed class & has direct access to the methods & members of the enclosed instance.
  • For example,  
    OuterClassNonStat.NonStaticNestedClass inner = outer.new NonStaticNestedClass();
 Example for Inner Class:
 class OuterClassNonStat{  
      // Declare static nested inner classs  
      class NonStaticNestedClass{  
           //non static inner class method  
           void display(){  
                System.out.println("Inside Non Static Nested Class");  
           }  
      }  
      //outer class method  
      void display(){  
           System.out.println("Outer Class");  
      }  
 }  
 class MainClassNonStatic  
 {  
      public static void main(String[] arg)  
      {  
           //how to access non static nested class method  
           //1st create object for outer class object  
           OuterClassNonStat outer = new OuterClassNonStat();  
           outer.display();  
           //Now calling non static nested class   
           OuterClassNonStat.NonStaticNestedClass inner = outer.new NonStaticNestedClass();  
           inner.display();  
      }  
 }  

 3. Local Inner Class:
  • Local inner class is class declared in the body of method. Such class is know within contains method only. It is called Local Inner Class.
  • If we are declaring local inner class inside method, we can access both instance and static variables of outer class directly from the inner class.
  • From local inner class we are not allowed to access the local variables of the method unless it is marked as final.
  • Its not a member of enclosing class, hence doesn't have a access specifiers.
  • These inner class are very rarely used classes in real time. Because, these inner classes can be accessed with in the method only. Outside the method, your not allowed to access, violation leads to CTE.
Example for local inner class:
 class OuterClass {  
   static int x=10;  
   void m1()  
   {  
           int y=20;  
           class LocalInnerClass  
           {  
                void show()  
                {  
                     System.out.println(x); // 10  
                     System.out.println(y); // Compile time error ( If y is final no error)  
                }  
           }  
           LocalInnerClass inner=new LocalInnerClass();  
           inner.show();  
   }  
 }  
 public class MainLocalInnerClass {  
      public static void main(String args[]) {  
           OuterClass outer = new OuterClass();  
     outer.m1();  
      }  
 }  

4. Anonymous Inner Class:
  • We can declare anonymous inner class either in static or non static methods.
  • If we declare anonymous inner class in the static method the static variables of outer class can be accessed directly from inner class.
  • If we are declaring anonymous inner class inside instance method, we can access both instance and static variables of outer class directly from the inner class.
  • From anonymous inner class we are not allowed to access the local variables of the method unless it is marked as final.
  • It is accessible only at the point of its declaration.
 Example for anonymous inner class:
 interface Inter {  
      void display();  
 }  
 class AnonymousInnerClass  
 {  
      public static void main(String arg[]){  
           //Create thread using anonymous class  
           int x = 10;  
           Runnable r=new Runnable(){  
                public void run(){  
                     System.out.println("run"+x);//compile error.  
                               //local variable x accessed from inner class (if x is final then no error)  
                }  
           };  
           Thread t=new Thread(r);  
           t.start();  
           //implement interface  
           Inter inter = new Inter(){  
                public void display(){  
                     System.out.println("Display method");  
                }  
           };  
           inter.display();  
      }  
 }  

Live Trend: Earthquake in Chennai, Thiruvallur Today Apr 11-4-...

Live Trend: Earthquake in Chennai, Thiruvallur Today Apr 11-4-...: - Mild Earthquake in some parts of Chennai at around 2.15 pm IST today April 11 2012 (Wednesday) - Tremor was also felt at Tiruvallur - Pe...

Difference between a Vector and an Array in Java


  1. Vector is a dynamic array, whose size can be increased, where as an array size can not be changed.
  2. Reserve space can be given to Vector, where as array can not.
  3. Vector is a Class, where as array is not.
  4. Vector can be stored any type of objects, where as an array can stored only homogeneous values.
  5. Vector implements List interface, where as an array is primitive data type.
  6. Vector is synchronized, where as array is not.

Difference between class variable and instance variable in java


Consider the following class:
public class IdentifyMyParts {
    public static int x = 7;
    public int y = 3;
} 
  1. Question: What are the class variables?
    Answer: x
  2. Question: What are the instance variables?
    Answer: y
  3. Question: What is the output from the following code:
    IdentifyMyParts a = new IdentifyMyParts(); 
    IdentifyMyParts b = new IdentifyMyParts(); 
    a.y = 5; 
    b.y = 6; 
    a.x = 1; 
    b.x = 2; 
    System.out.println("a.y = " + a.y); 
    System.out.println("b.y = " + b.y); 
    System.out.println("a.x = " + a.x); 
    System.out.println("b.x = " + b.x); 
    System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
    
    Answer: Here is the output:
     a.y = 5 
     b.y = 6 
     a.x = 2 
     b.x = 2
     IdentifyMyParts.x = 2
    
    Becausexis defined as apublic static intin the classIdentifyMyParts, every reference toxwill have the value that was last assigned becausexis a static variable (and therefore a class variable) shared across all instances of the class. That is, there is only onex: when the value ofxchanges in any instance it affects the value ofxfor all instances ofIdentifyMyParts

JSP useBean and Scope Types

The beans can be declared in JSP as following: Scope parameter indicates the context in which the bean should be made available. There are four different possible kinds of useBean scope, “page” scope being the default one.
  1. page: Availability: The bean is only available on the current page Storage:The bean is stored in the PageContext of the current page.
  2. request: Availability: The bean is only available for the current client request Storage: The bean is stored in the ServletRequest object. 
  3. session: Availability:The object is available to all pages during the life of the current HttpSession. Storage: The bean is stored in HttpSession object. 
  4. application: Availability: The bean available to all pages that share the same context. Storage: The bean is stored in ServletContext object

How do handling String object in JVM?

  1. Java Virtual Machine maintains an internal list of references for interned Strings (Pool of unique Strings) to avoid duplicate String objects in heap memory.
  2. Whenever the JVM load String literal from the class file and executes, it checks whether that String already exists in the internal list or not.
  3. if Already exists in the list, String object does not create a new String and it uses reference to existing String  object.
  4. JVM does this type of checking for String literal but not for String object which it creates through 'new' keyword.
  5. You explicitly force JVM to do this type of checking for String objects which are creates through 'new' keyword using String.intern() method.
  6. This forces JVM to check the internal list and use the existing String object if it is already exist.

Difference between String and StringBuffer in java

Overview of String:
  1. String is Immutable objects.
  2. Immutable objects cannot be modified once they are created.
  3. Next question that comes to our mind is "If String is immutable than how i change the contents of the object?".
  4. Well, to be precise it’s not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.
Example :
      String myString = "Hello";
      Next, you want append "Guest" to the same String. What do you do?
      myString = myString + " Guest";

When you print the content of myString the output will be "Hello Guest".
Although we made use of the same object(myString), internally a new object was created in the process. 
Here "+" is act concatenation operation in String. 

Example:
      String myStr = "Hello".
      Next, you want append "Guest" to the same String using String's concat() method.
      myStr.concat(" Guest");

When you print content of the object(myStr) the output will be "Hello".
Here not call internal a new object. So, you assign explicitly another String object or same String object.

     String tempStr =  myStr.concat(" Guest");
Now, you print the content of  tempStr the output will be "Hello Guest".

Overview of StringBuffer:
  1. StringBuffer is mutable objects.
  2. Mutable objects can be modified after their creation.
The default behavior of StringBuffer:
  1. StringBuffer maintains a character array internally.
  2. When you create StringBuffer with default constructor StringBuffer() without setting initial length, then the StringBuffer is initialized with 16 characters.
  3. The default capacity of 16 characters.
  4. When the StringBuffer reaches its maximum capacity , it will be increase its size by twice the size plus  (2 * old size) + 2.
Example:
       StringBuffer strBuf = new StringBuffer();
       Next, you want append " to Java" to the same object
       strBuf.append("Welcome");

When you print the content of strBuf the output will be "Welcome to Java".