Friday, June 29, 2012

Exception & Runtime Stack Mechanism

Exception:-

An unwanted event that (unexpected) disturbs the normal flow of the program is called an Exception.
Example: FileNotFoundException, SleepingException, BombblastException.
  1. It is always a good programming practice to handle exceptions.
  2. The objective of the exception handling is for graceful termination of the program.
  3. Exception handling does not mean, repairing an exception.  We are defining alternative way to continue rest of code normally.  This is called Exception handling.
Example:  If our program requirement is, we have to read the data from remote file locating at London at runtime; if the file is not available we have to use local file alternatively to continue the rest of the code normally, this is nothing but Exception Handling.

Run time stack mechanism:-
  1. For every thread JVM creates one runtime stack, all the method calls which are performed by that thread will be stored into the corresponding stack.  Each record (entry) in the stack is called activation record or stack frame.
  2. If a method completed normally then the entry from the stack will be removed.  After completing the entire method calls stack is empty, just before terminating the thread JVM destroys the corresponding stack.


Example:                        

class Test 
{
public static void main(String[] args){
doStaff();
}
public static void doStaff(){
doStaffMore();
}
public static void doStaffMore(){ 
System.out.println("Hello");
}
}

Default Exception Handling:-

  1. If an exception is raised, the method in which occurs is responsible for creation of an exception object by including the following information
  2.  After creating exception object the method handovers that exception object to JVM.
  3. JVM checks, is there any exception handling code is available or not.
  4. If it is not available JVM terminates that method abnormally and corresponding entry from the stack will be removed.
  5. Then JVM checks for exception handling code in the caller method, if the caller method also doesn’t contain exception handling code then JVM terminates that caller method abnormally and the corresponding entry from the stack will be removed.
  6. This process will be continued until main method and if the main method also does not contain exception handling code JVM terminates main method abnormally and the corresponding entry from the stack will be removed.
  7. Just before terminating the program abnormally JVM handovers the responsibility of exception handling to the default exception handler which is the component of JVM.
  8. Default Exception Handler prints that exception information in the console as follows.
  • Name of the Exception
  • Description of exception
  • Location of Exception (Stack trace)
Example:
Exception in thread “main” java.lang.ArithmeticException:  / by zero.
At Test.doStaffMore (Test.java 13)
At Test.doStaff (Test.java)
At Test.main()

Java Tutotials

Monday, June 25, 2012

Need of a Servlet.

Web Programming for static Information

  1. Client sends a request for a static file. 
  2. Server searches whether the requested static file is available or not 
  3. If the requested static file is available then web server provides that file as the response otherwise, it provides 404 status code saying requested resource is not available.
Note:   To serve static file no process is required at server side, hence webserver always love to serve static information as response.




Web Programming for dynamic Information


  1. Client sends a request. 
  2. If the request is for static files then,server searches whether the requested static file is available or not.
  3. If the requested static file is available then web server provides that file as the response otherwise, it provides 404 status code saying requested resource is not available. 
  4. If the request is for dynamic information then web server forwards that request to some helper application. 
  5. Helper application analyses the request and process the request and generates the required dynamic information. 
  6. Helper application forwards that dynamic response to the web server and web server intern forwards that response to the client.

The following are the various helper applications available in the market.
                Servlet, jsp, asp, asp.net, cold fusion, php, SSJS, CGI
For the generation of dynamic data we need the helper application as shown in the above diagram.  Our Servlet acts as a helper application to generate the dynamic responses to the clients request.
                To run this helper application it needs some environment.  That environment will be provided by the web container. Web container is responsible for maintaining these helper applications.
Now we can say a servlet is:
Servlet is a server side web component managed by the web container that generates the dynamic responses.