Sunday, April 25, 2010

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. 

Wednesday, April 21, 2010

HashSet with Example


If your 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();
    }
}

Sortedset with Example


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);
}
}
}

What is the difference between Iterator and ListIterator?

Iterator:
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());
 
  }
}
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 :)

Monday, April 19, 2010

Developing Charts with JFree Charts

Implementing the charts while developing the Web applications as well as Stand alone applications is crucial part. This must act like business intelligence and it should give more information to the user with small graph or in short form of data. These will play major part in the application for analyzing and capturing application whole information.

For example A shopping cart applications sells the things in online and the owner of shopping cart wants to know the sales details in daily basis or weekly basis or Monthly basis and so on... and he has to get this information in short way and user friendly. He may need the more information like in which country the things are selling more, which materials are selling more and the things which are added for the carts and so on... In these situations representing the data in the graphical format will make the user to feel comfortable and feel like friendly.

As I from Java technology I worked more around to choose open source chart implementing tools for some days. I looked into Jasper Reports, Jfree charts, and some other technologies. Jasper reports are more useful while displaying the data in pdf fomat or excel  format and other formats. This has the ui tool called iReport to define the jrxml files and these jrxml files are parsed and finally we will get the data. This data can be displayed on different formats like pdf, excel,html...etc. Using JFree charts we can display the reports data in a graphical format. This chart can be write into pdf or other formats using JFree api. Here i will explain briefly about JFee charts.

By visiting the www.jfree.org site we can see the Jfree charts features as:
" JFreeChart is a free 100% Java chart library that makes it easy for developers to display professional quality charts in their applications.
  • a consistent and well-documented API, supporting a wide range of chart types;
  • a flexible design that is easy to extend, and targets both server-side and client-side applications;
  • support for many output types, including Swing components, image files (including PNG and JPEG), and vector graphics file formats (including PDF, EPS and SVG);
  • JFreeChart is "open source" or, more specifically, free software. It is distributed under the terms of the GNU Lesser General Public Licence (LGPL), which permits use in proprietary applications. "
The demo application can be downloaded from the site and u can start implementing the JFreechart's applications. JfreeChart developer guide helps more to develop the applications.

As per my opinion JFreeChart is more friendly for the developers while developing the applications. Using this you can implement Pi, Bar, StackedBar, Line, Bubble, Step ..etc charts.

Some times you may need to display JFreeChart image in JSP in that case Cewolf tag library helps more. Using Cewolf Tag library you can display all types charts in JSP and its easy to display tool tip, link and labels.

If you works with JSF, you can use Primefaces or Rich faces to integrate the JFree charts and its easy to implement.

Friday, April 9, 2010

Prime faces

Kowing about prime faces is more worth while developing the Web applications wusing JSF framework. Primefaces is just like a plugin for the JSF. It is more related to the UI of the application. Adding this plugin to the JSF based application gives confidence developer in designing ui interface in competence with other best ui technology.

Its providing more features like AccordianPannel, AutoComplete, Captua, Calender, Charts, Color picker,CommandButton, CommandLink, Confim Dialog, Data Exporter, Data Table, File Upload, File Download, Graphic Image, Image Compare, Layout, Menu, Menu Bar, Panel, PickList, Stack, Stack Menu,Tab, Tab Slider, Tree, Tool Tip ... etc.

Its providing more features related to JavaScript and extention to the JSF tags.

Integrating the Prime faces with JSF applications is too easy, its just adding the jar file into the application and setting up normal requirements in the web.xml file.