What is the difference between EAR, JAR and WAR file?


In J2EE, application modules are packaged as EAR, JAR and WAR based on their functionality.

Each type of file (.jar, .war, .ear) is processed uniquely by application servers, servlet containers, EJB containers, etc

JAR:

Jar file (file with a .jar extension) is the normal Java Application archive and intended to hold generic libraries of Java classes, resources, auxiliary files, etc

This can be added in classpath for compilation and to run java program. Generally in web applications we keep these files in lib directory of the application.

Ex: ojdbc14.jar – This contains all the classes to connect the oracle database

Servlet-api.jar – contains servlet related classes

WAR:

Web modules which contains Servlet class files,JSP Files,supporting files, GIF and HTML files are packaged as JAR file with .war( web archive) extension

War files (files with a .war extension) are intended to contain complete Web applications. In this context, a Web application is defined as a single group of files, classes, resources, .jar files that can be packaged and accessed as one servlet context.

EAR:

All above files(.jar and .war) are packaged as JAR file with .ear ( enterprise archive) extension and deployed into Application Server.

Ear files (files with a .ear extension) are intended to contain complete enterprise applications. In this context, an enterprise application is defined as a collection of .jar files, resources, classes, and multiple Web application.

ListIterator with an example


ListIterator allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator’s current position in the list. A ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous() and the element that would be returned by a call to next().

Example:
 public class sample {
 
  public static void main(String[] args) {
    //create an object of ArrayList
    ArrayList aList = new ArrayList();
 
    //Add elements to ArrayList object
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
 
    //Get an object of ListIterator using listIterator() method
    ListIterator listIterator = aList.listIterator();
 
      
    System.out.println(" forward direction using ListIterator");
    while(listIterator.hasNext())
      System.out.println(listIterator.next());
 
   
    System.out.println("reverse direction using ListIterator");
    while(listIterator.hasPrevious())
      System.out.println(listIterator.previous());
   }
}
 
try the above code and get back to me :)

Iterator with an example


Using Iterator we can iterate only in forward direction and you cannot add elements while iterating and Here cursor always points to specific index.

Example:
 public class Example {
 
  public static void main(String[] args) {

    ArrayList aList = new ArrayList();
 
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");
 
    Iterator itr = aList.iterator();
 
     while(itr.hasNext())
      System.out.println(itr.next());
 
  }
}

When to go for Sortedset


When you want to sort the data we have to go with sortedset by default the sorting order is ascending.
You can customize the sorting order by Implementing comparator interface.
You can use treeset for sorting and for this you need to use the constructor which accepts comparator as argument.
Example:

public class Example {
public static void main(String[] args) {
SortedSet ss=new TreeSet();

ss.add(“a”);
ss.add(“e”);
ss.add(“g”);
ss.add(“b”);
ss.add(“c”);

Iterator it=ss.iterator();

while(it.hasNext())
{
String value=(String)it.next();
System.out.println(“Value :”+value);
}
}
}

HashSet with Example


If  our requirement is frequent insert then we go with HashSet.
For efficiency objects added to a HashSet need to implement the hashCode() method in a manner that properly distributes the hash codes. While most system classes override the default hashCode() implementation of the Object.
It is generally faster to add elements to the HasSet then convert the collection to a TreeeSet for sorted traversal.
To optimize HashSet space usage , you can tune initial capacity and load factor.
Example:
public class HashSetExample {

    public static void main(String[] args) {

        HashSet<String> hs=new HashSet<String>();

        // duplicate element is not permitted
        hs.add("b");
        hs.add("a");
        hs.add("c");
        hs.add("d");
        hs.add("d");

        Iterator it=hs.iterator();

        while(it.hasNext())
        {
            String value =(String)it.next();

            System.out.println("Value :"+value);
        }

        System.out.println("Size :"+hs.size());

        hs.remove("d");

        hs.clear();
    }
}

Which is faster in execution-JSP or SERVLET ? Why?


Servlet is faster in execution as every jsp is changed on converted to servlet before starting the execution. Thus the server need to spend some time to convent a JSP file to servlet at it’s first time execution. But developing a jsp is very easy when compared to servlet.
Every time before executing a jsp & server searches for related servlet file. If it existsWhen ever we send a request to the JSP file it checks the servlet representing the JSP file. Then the web container runs the jsp compiler that  generates the servlet code
then start the execution of that servlet otherwise server creates servlet files for those jsps first and then execution takes place

Can we declare final method in abstract class?


If a method in a class is defined as final, then we can’t provide the re-implementation for that final method in it’s derived classes i.e overriding is not possible for that method.

An abstract class contains one or more abstract method.

  • We can have concrete(non-abstract) methods declared as final as it contains both definition and declaration in abstract itself.
  • However, we cannot define abstract methods as final, since the class inheriting our abstract class cannot define/re-implement them.

Example:

abstract class Shape
{
    protected String name;

    abstract void draw();
    abstract void paint();

    final String getNameOfShpae() {
        retrun "Name of the Shape is"+name;
    }
}

 

Can the same object os Stateless Session bean be shared by multiple clients? Can the same object os Stateful Session bean be shared by multiple clients?


It is possible to share the same object of stateless session bean by multiple clients.
A Server maintains a pool of objects in case of stateless session bean. And every When ever we send a request to the JSP file it checks the servlet representing the JSP file. Then the web container runs the jsp compiler that  generates the servlet code
time for a request it uses one of the pool of objects which is free at that time. The server gets back that object in to the pool only after the client work on that object is Directoryover [ client says that by invoking ejbremove() method]

After the client release that object, the server may used the same object to handle another client request, thus the same object to handle another client request. Thus the same object of stateless session can be shared by multiple clients But sharing the same object for multiple clients is not possible in case of state full session bean. Here server should maintain an object for every clients to handle / maintain the state of that object.

How we declare a synchronized block? What does this represent in the Declaration of sunchronized block?


When two or more threads need to access to a shared resource they need some way to ensure that the resource will be used by only one thread at a time. The process by which this achieved is called synchronization.

The general form of synchronize statement :

Synchronized (object) {
// statement to be synchronized
}
here the object is a reference to the object being synchronized. A synchronized block ensures that a call to a method that is member of object occurs only after the current thread has successfully entered the object’s moniter