Friday, March 26, 2010

What are implicit objects?

Implicit Objects:

Implicit Objects in JSP are objects that are automatically available in JSP. Implicit Objects are Java objects that the JSP Container provides to a developer to access them in their program using JavaBeans and Servlets. These objects are called implicit objects because they are automatically instantiated.

Following are the implicit Objects

request
response
session
application
page
pageContext
out
config
exception


What is difference between HashMap and HashTable and HashSet and TreeSet?

Hashtable
Hashtable is basically a data structure to retain values of key-value pair
  •  It didn’t allow null for both key and value. You will get NullPointerException if you add null value.
  • It is synchronized. So it comes with its cost. Only one thread can access in one time. So It is slow.
HashMap
Like Hashtable it also accepts key value pair.
  • It allows null for both key and value. 
  • It is unsynchronized. So come up with better performance(fast)

HashSet
It is from Set family.
  • HashSet does not allow duplicate values. 
  • It provides add method rather put method. 
  • You also use its contains method to check whether the object is already available in HashSet. 
  • HashSet can be used where you want to maintain a unique set but not the order. 


TreeSet
 It is also from Set family.
  • Likely HashSet, TreeSet does not allow duplicate values.
  • It provides add method rather put method. 
  • You also use its contains method to check whether the object is already available in TreeSet. 
  • TreeSet can be used where you want to maintain a unique set as well as the order. 



Monday, February 15, 2010

JSP Life Cycle

1. JSP Page Translation:

A java servlet file is generated from the JSP source file. This is the first step in its tedious multiple phase life cycle. In the translation phase, the container validates the syntactic correctness of the JSP pages and tag files. The container interprets the standard directives and actions, and the custom actions referencing tag libraries used in the page.

2. JSP Page Compilation:

The generated java servlet file is compiled into a java servlet class.
Note: The translation of a JSP source page into its implementation class can happen at any time between initial deployment of the JSP page into the JSP container and the receipt and processing of a client request for the target JSP page.

3. Class Loading:

The java servlet class that was compiled from the JSP source is loaded into the container.

4. Execution phase:

In the execution phase the container manages one or more instances of this class in response to requests and other events.
The interface JspPage contains jspInit() and jspDestroy(). The JSP specification has provided a special interface HttpJspPage for JSP pages serving HTTP requests and this interface contains _jspService().

5. Initialization:

jspInit() method is called immediately after the instance was created. It is called only once during JSP life cycle.

6. _jspService() execution:

This method is called for every request of this JSP during its life cycle. This is where it serves the purpose of creation. Oops! it has to pass through all the above steps to reach this phase. It passes the request and the response objects. _jspService() cannot be overridden.

7. jspDestroy() execution:

This method is called when this JSP is destroyed. With this call the servlet serves its purpose and submits itself to heaven (garbage collection). This is the end of jsp life cycle.
jspInit(), _jspService() and jspDestroy() are called the life cycle methods of the JSP.

Friday, February 12, 2010

Servlet Life Cycle

Servlet Life Cycle Methods

The following are the life cycle methods of a servlet instance:
  • init()
  • service()
  • destroy()
We will look into the each method in detail.

init()

This method is called once for a servlet instance. When first time servlet is called, servlet container creates instance of that servlet and loaded into the memory. Future requests will be served by the same instance without creating the new instance. Servlet by default multithreaded application.init() method is used for inilializing servlet variables which are required to be passed from the deployment descriptor web.xml. ServletConfig is passed as the parameter to init() method which stores all the values configured in the web.xml. It is more convenient way to initialize the servlet.

service()

This method is called for the each request. This is the entry point for the every servlet request and here we have to write our businesslogic or any other processes. This method takes HttpServletRequest and HttpServletresponse as the parameters. It is not mandatory to write this method, normally developers are interested in writing doGet() or doPost() methods which is by default called from the service() method. If you override service(), it is your reponsibility to call the appropriate methods. If you are not overridden the service() method, based on the types of the request the methods will be called.

destroy()

This method will be called once for a instance. It is used for releasing any resources used by the servlet instance. Most of the times it could be database connections, Fill IO operations, etc. destroy() is called by the container when it is removing the instance from the servlet container. Servlet instance is deleted or garbage collected by the container only when the web server issues shut down or the instance is not used for a long time.


What is difference between final and finally and finalize?

final:
  1. final keyword can be applied to variables,method,class. 
  2. final variable - You can't reassign/modify values to the variables. final class- You cannot extends(inherit) the class.
  3. final method- You cannot override the final methods.
finally:
  1. finally is used in try-catch (i.e exception handling in java).
  2. Each try contain only one finally blocks not more than one.
  3. There is no statement between catch block and try block. It muse execute if exception is occurred or not. Mostly used for memory release (closes connections).

finalize():
  1. This is method used to release the occupied memory.
  2. finalize() method must be protected or public otherwise compile time error.

What is difference between Overloading and Overriding?

Overloading: The methods with the same name but it differs by types of arguments and number of arguments.

Overriding:  The methods with the same name and same number of arguments and types but one is in base class and second is in derived class.


What is difference between Unchecked and Checked Exceptions?

Unchecked exceptions :
  • represent defects in the program (bugs) - often invalid arguments passed to a non-private method. To quote from The Java Programming Language, by Gosling, Arnold, and Holmes : "Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time."
  • are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException
  • a method is not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so)
Checked exceptions :
  • represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)
  • are subclasses of Exception
  • a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow)