Sunday, 20 July 2014

What are the Implicit Objects in JSP?

             JSP Implicit Objects are the Java objects that the JSP Container makes available to developers in each page and developer can call them directly without being explicitly declared. JSP Implicit Objects are also called pre-defined variables.

    JSP supports nine Implicit Objects which are listed below:

 1) request :
            This is the HttpServletRequest object associated with the request.

 2) response :
           This is the HttpServletResponse object associated with the response to the client.

 3) out :
         This is the PrintWriter object used to write any data to buffer.

 4) session :
         This is the HttpSession object associated with the request.

  5) application :
         This is the ServletContext object associated with application context.

  6) config :
         This is the ServletConfig object associated with the page.

  7) pageContext :
        This encapsulates use of server-specific features like higher performance JspWriters.

  8) page :
       This is simply a synonym for this, and is used to call the methods defined by the
      translated servlet  class.

  9) Exception :
         The Exception object allows the exception data to be accessed by designated JSP.

Sunday, 13 July 2014

Difference between HashMap and Hashtable in collection in Java

There are several differences between HashMap and Hashtable in Java:
  1. The HashMap class is roughly equivalent to Hashtable, except that it is non synchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn't allow nulls).   
  2.  HashMap does not guarantee that the order of the map will remain constant over time.
  3. HashMap is non synchronized whereas Hashtable is synchronized.
  4. Iterator in the HashMap is fail-safe while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other Thread modifies the map structurally by adding or removing any element except Iterator's own remove() method. But this is not a guaranteed behavior and will be done by JVM on best effort.
Note on Some Important Terms
  1. Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.
  2. Fail-safe is relevant from the context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the collection object "structurally", a concurrent modification exception will be thrown. It is possible for other threads though to invoke "set" method since it doesn't modify the collection "structurally". However, if prior to calling "set", the collection has been modified structurally, "IllegalArgumentException" will be thrown.
  3. Structurally modification means deleting or inserting element which could effectively change the structure of map.
HashMap can be synchronized by
Map m = Collections.synchronizeMap(hashMap);

            Map provides Collection views instead of direct support for iteration via Enumeration objects. Collection views greatly enhance the expressiveness of the interface, as discussed later in this section. Map allows you to iterate over keys, values, or key-value pairs; Hashtable does not provide the third option. Map provides a safe way to remove entries in the midst of iteration; Hashtable did not. Finally, Map fixes a minor deficiency in the Hashtable interface. Hashtable has a method called contains, which returns true if the Hashtable contains a given value. Given its name, you'd expect this method to return true if the Hashtable contained a given key, because the key is the primary access mechanism for a Hashtable. The Map interface eliminates this source of confusion by renaming the method containsValue. Also, this improves the interface's consistency — containsValue parallels containsKey.

Related Post:
 1) Collection Interview Qusetions and Answers
 2) How HashMap works internally in java?