Learn Servlets by Example

Written by admin on . Posted in Learn By Example

Here you will find several examples related to Servlet which will help you start developing servlet and understand the functioning of Servlets.

  • How to Create Simple Web Application for Servlets in Eclipse?
  • How to Write Hello World Servlets?
  • How to map Servlet to URL?
  • How to use requestDispathcer in Servlet?
  • How to Write Servlet in Eclipse?
  • How to test servlets?
  • How to control Servlet loading sequence in container?
  • How to foward control from Servlet to JSP?
  • How to send control from Servlet to JSP?
  • How to load Servlet on startup?

How to create final class in java

Written by admin on . Posted in Learn By Example

Here we will learn to write Final class in Java and how to get the Object of final class. One of Example of Final class in Java is String Class. Creating final class is mainly useful when you try to create the Immutable class(Like String).

Once declared final, Class can not be sub classed. Hence no method can ever be overridden. So even it is not required but if you make any method as Final it will not create any problem. Final keyword can be used @ Class level to create Final class.

Order of execution of blocks in java

Written by admin on . Posted in Learn By Example

Here we will learn to see the order of execution of blocks in Java.

Different block in Java Class

  • Static Block
  • Init Block(Anonymous Block)
  • Constructor
/*
 * Here we will learn to see how the different part (Ananymous Block, Constructor and Static Block ) of class will behave
 * and what would be the order of execution. 
 */
class JBTCLass {

	/*
	 * Here Creating the Ananymous Block
	 */
	{
		System.out.println("Inside Ananymous Block");
	}

	/*
	 * Now Creating the Static Block in Class
	 */
	static {
		System.out.println("Inside Static Block");
	}

	/*
	 * Here Creating the Constructor of Class
	 */
	JBTCLass() {
		System.out.println("Inside Constructor of Class");
	}

	public static void main(String[] args) {

		// Creating the Object of the Class
		JBTCLass obj = new JBTCLass();

		System.out.println("*******************");

		// Again Creating Object of Class
		JBTCLass obj1 = new JBTCLass();

	}

}

Output of the above programme

Inside Static Block
Inside Ananymous Block
Inside COnstructor of Class
*******************
Inside Ananymous Block
Inside COnstructor of Class

As you can see STATIC block will execute only once when class get loaded. But Anonymous block and Constructor will execute every time object of a Class gets created.

How to create Method local Inner class

Written by admin on . Posted in Learn By Example

/*
 * Here we will learn to create method local inner class & how to access method inside
 * Method local inner class.
 */
public class MethodLocalInnerClassExample {

	void OuterClassMethod() {

		/*
		 * Method local Inner class name should not match the name of Outer
		 * class Otherwise it compiler will give an error.
		 *
		 * The nested type <Outer Class Name> cannot hide an enclosing type
		 */
		class MethodLocalInnerClass {

			void InnerClassMethod() {
				System.out.println("Inside method local inner class");
			}
		}
		MethodLocalInnerClass obj = new MethodLocalInnerClass();
		obj.InnerClassMethod();
	}

	public static void main(String[] args) {
		MethodLocalInnerClassExample obj = new MethodLocalInnerClassExample();
		obj.OuterClassMethod();

	}

}