Friday, July 24, 2015

Developing JSF UI

JSF technology uses Tag Libraries containing simple and reusable Tags to create rich and dynamic UI component model. You can implement validations, conversions and handle events generated by UI.

Basic Two types of JSF Tag Libraries are

  • HTML Tag Library
  • Core Tag Library.
  1. HTML TAG LIBRARY:
You can create UI form components using HTML Tag Library. To use HTML Tag Library , you need to include a namespace in your WebPage(.xhtml file).
"http://xmlns.jcp.org/jsf/html"
  • Head and Body Tag:
          Head and body tag is included between < html > and < /html > tag.
         For Example.

          < html xmlns="http://www.w3.org/1999/xhtml"
                    xmlns:h="http://xmlns.jcp.org/jsf/html"  >
          < h:head >
                           < title >  WELCOME TO JSF FIRST PAGE < /title >
          < /h:head >
          < body >
                            To do Content.....you can design your webpage here.
           < /body >
          < /html >
2. Form Tag:
       Jsf form tag uses Postback method to submit form data to server, hence uses POST method.
       You can include form tag in body tag.
For Example:
< html xmlns="http://www.w3.org/1999/xhtml"
                    xmlns:h="http://xmlns.jcp.org/jsf/html"  >
          < h:head >
                           < title >  WELCOME TO JSF FIRST PAGE < /title >
          < /h:head >
          < body >
                         < h:form  id="myform1" >
                                       < !-- other UI  components -- >
                        < /h:form >
           < /body >
          < /html >

TO BE CONTINUED.....




Friday, January 2, 2015

JSF

JSF is a framework that is used for developing interactive and secure Web Applications.
It provides built-in components, such as textboxes, labels, radio buttons, and command buttons for UI(user Interface) creation. These components are referred to as UI creation.
JSF Framework  provides built-in support for converting and validating user data .
It provides navigation and event handling mechanisms.It is based on popular Model-view-controller pattern.
It enables the developers to easily segregate the UI(view),user data(model),and the code(controller) that controls the navigation between user data and UI.
JSF can be used   to create UI component and can implement the logic to process user data,handle events and decide which page to be displayed next.
It is best suited for presentation tier of an enterprise application.
It provides event model that uses simple java classes (similar to Swing) for dispatching event from client-side UI component to server.
What is difference between JSF and Swing?
JSF framework is used to create UIs for Web Applications that execute on server however Swing is a collection of Java APIs that enables the user to create UIs for desktop –based application.
JSF is used to provide a mechanism of automatic generation of appropriate for the target client such as html and wml(wireless markup language) browsers.
JSF and Struts both are based on MVC pattern. The design goal of struts framework is used to separate UI, business logic and controller. The design goal of JSF is to create UI.
JSF TAG LIBRARY:
There are two types of tag Library:
  •              JSF HTML tag library
  •              JSF core tag library

HTML Tag Library:
It is used to create UI component for Web Application easily. you can also perform simple validation on user entered data.
To use html tag library you have to use namespace : http://java.sun.com/jsf/html in your webpage.
  • Head tag : it is used within html tag and is used to describe document.

        < h:head >  is used to denote head tag.
  • Body tag: used to set boundaries of html document. h:body > is used to denote body tag.
  • Form Tag: It is used to denote a form in html page.h:form > is used for form tag.

  • Input Tags:They are used for editable text UI components.

  1.  < h:inputText > : creates a textbox
  2.  h:inputTextArea :create a multiline textbox.
  3. h:inputSecret > :creates a password box
  4. h:inputhidden > :creates a hidden input to store users data.
  • Output Tags:

They are used for read-only UI component.
  1. h:outputLabel : creates a Text Label to display.
  2. h:outputText : creates a non-editable Text component
  3.  h:outputLink : creates a hyperlink to navigate to another webpage
  4. h:outputFormat :creates a formatted text component.
  5. h:graphicImage > : creates a UI to display Image.



  • Command Tags:

They are used to perform an action when they are activated.
a.     h:commandButton :  creates a button control
b.     h:commandLink >: creates a link.
  • Selection Tags:

They are used to select one or more than one values from given list of values.
  • SelectOne Components:

a.     h:selectBooleanCheckbox >: creates a checkbox to select only one value.
b.     h:selectOneMenu > : creates a drop-down list.
c.      h:selectOneListBox : creates a listbox
d.     h:selectOneRadio : creates a radiobutton.
  • SelectMany Components:

a.     h:selectManyCheckbox : creates a checkbox for multiple selection
b.     h:selectManyMenu > :creates a drop-down list for multiple selection
c.      h:selectManyListBox : creates a listbox for multiple selection.


Monday, September 17, 2012

Java Applets


Java Applets:


A Java applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a web page and runs in the context of a browser. An applet must be a subclass of the java.applet.Applet class. The Applet class provides the standard interface between the applet and the browser environment.

Every Java applet must define a subclass of the Applet or JApplet class. In the Hello World applet, this subclass is called HelloWorld. The following is the source for the HelloWorld class.

import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;

public class HelloWorld extends JApplet {
    //Called when this applet is loaded into the browser.
    public void init() {
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    JLabel lbl = new JLabel("Hello World");
                    add(lbl);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
}
Java applets inherit significant functionality from the Applet or JApplet class, including the capabilities to communicate with the browser and present a graphical user interface (GUI) to the user.


To use above Applet in html file::

Friday, September 14, 2012

String Functions


Creating Strings:


To create a String from char Array:

public class StringDemo{
   public static void main(String args[]){
      char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
      String helloString = new String(helloArray);  
      System.out.println( helloString );
   }
}

String Length:

To find length of String str use following function:
str.length();

For Example:

public class StringDemo{
   public static void main(String args[]){
      String palindrome = "Dot saw I was Tod";
      int len = palindrome.length();
      System.out.println( "String Length is : " + len );
   }
}

Concatenating Strings:

str1.concat(str2);



SNMethods with Description
1char charAt(int index) 
Returns the character at the specified index.
2int compareTo(Object o) 
Compares this String to another Object.
3int compareTo(String anotherString)
Compares two strings lexicographically.
4int compareToIgnoreCase(String str) 
Compares two strings lexicographically, ignoring case differences.
5String concat(String str)
Concatenates the specified string to the end of this string.
6boolean contentEquals(StringBuffer sb) 
Returns true if and only if this String represents the same sequence of
characters as the specified StringBuffer.
7static String copyValueOf(char[] data) 
Returns a String that represents the character sequence in the array specified.
8static String copyValueOf(char[] data, int offset, int count)
Returns a String that represents the character sequence in the array specified.
9boolean endsWith(String suffix)  Tests if this string ends with the specified suffix.
10boolean equals(Object anObject) Compares this string to the specified object.
11boolean equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations.
12byte getBytes() 
Encodes this String into a sequence of bytes using the platform's default charset,
storing the result into a new byte array.
13byte[] getBytes(String charsetName
Encodes this String into a sequence of bytes using the named charset,
storing the result into a new byte array.
14void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.
15int hashCode() Returns a hash code for this string.
16int indexOf(int ch) 
Returns the index within this string of the first occurrence of the specified character.
17int indexOf(int ch, int fromIndex) 
Returns the index within this string of the first occurrence of the specified character,
 starting the search at the specified index.
18int indexOf(String str)
Returns the index within this string of the first occurrence of the specified substring.
19int indexOf(String str, int fromIndex)
Returns the index within this string of the first occurrence of the specified substring,
starting at the specified index.
20String intern() Returns a canonical representation for the string object.
21int lastIndexOf(int ch)  Returns the index within this string of the last occurrence of the specified character.
22int lastIndexOf(int ch, int fromIndex)
 Returns the index within this string of the last occurrence of the specified character,
searching backward starting at the specified index.
23int lastIndexOf(String str)
Returns the index within this string of the rightmost occurrence of the specified substring.
24int lastIndexOf(String str, int fromIndex)
Returns the index within this string of the last occurrence of the specified substring,
searching backward starting at the specified index.
25int length()  Returns the length of this string.
26boolean matches(String regex)
Tells whether or not this string matches the given regular expression.
27boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) 
Tests if two string regions are equal.
28boolean regionMatches(int toffset, String other, int ooffset, int len)
Tests if two string regions are equal.
29String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all occurrences
of oldChar in this string with newChar.
30String replaceAll(String regex, String replacement
Replaces each substring of this string that matches the given regular
expression with the given replacement.
31String replaceFirst(String regex, String replacement)
Replaces the first substring of this string that matches the given regular
expression with the given replacement.
32String[] split(String regex)
Splits this string around matches of the given regular expression.
33String[] split(String regex, int limit)
Splits this string around matches of the given regular expression.
34boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.
35boolean startsWith(String prefix, int toffset)
Tests if this string starts with the specified prefix beginning a specified index.
36CharSequence subSequence(int beginIndex, int endIndex)
Returns a new character sequence that is a subsequence of this sequence.
37String substring(int beginIndex)
Returns a new string that is a substring of this string.
38String substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string.
39char[] toCharArray()
Converts this string to a new character array.
40String toLowerCase()
Converts all of the characters in this String to
lower case using the rules of the default locale.
41String toLowerCase(Locale locale)
Converts all of the characters in this String to
 lower case using the rules of the given Locale.
42String toString()
This object (which is already a string!) is itself returned.
43String toUpperCase()
Converts all of the characters in this String to
upper case using the rules of the default locale.
44String toUpperCase(Locale locale)
Converts all of the characters in this String to
upper case using the rules of the given Locale.
45String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.
46static String valueOf(primitive data type x)
Returns the string representation of the passed data type argument.

Thursday, September 13, 2012

PACKAGE

package is a collection of related classes.It helpsOrganize your classes into a folder structure and make it easy to locate and use them.
More importantly,It helps improve re-usability.
SYNTAX::
package ;
Example:
package Mypack;
//*** Mypack is name of folder in which your file should be saved.

Steps to create a package::

  • create a folder name MyPack
  • open your editor or notepad and type following class in MyPack package:
package MyPack;
public class Amount
{
                public double calamt(double price,double qty)
                {
                                    return(price*qty);
                 }
}
  • save above file in MyPack folder.
  • open a new notepad file and type following code to import above class Amount
import MyPack.Amount;
public class Bill
{
           public static void main(String args[])
           {
                    Amount ob = new Amount();   //object of class Amount
                    double amt = ob.calamt(150.5,2);
                   System.out.println("Please pay Rs. "+amt); 
           }
}

  • save above file outside MyPack folder. Compile and Run the file.





Interfaces

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.


An interface is similar to a class in the following ways:
  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The bytecode of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.

    However, an interface is different from a class in several ways, including:
    • You cannot instantiate an interface.
    • An interface does not contain any constructors.
    • All of the methods in an interface are abstract.
    • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
    • An interface is not extended by a class; it is implemented by a class.
    • An interface can extend multiple interfaces.



      FOR EXAMPLE::


      interface Animal {
      
       public void eat();
       public void travel();
      }
      
      
      
      
      
      
      public class MammalInt implements Animal{
      
         public void eat(){
            System.out.println("Mammal eats");
         }
      
         public void travel(){
            System.out.println("Mammal travels");
         } 
      
         public int noOfLegs(){
            return 0;
         }
      
         public static void main(String args[]){
            MammalInt m = new MammalInt();
            m.eat();
            m.travel();
         }
      } 

Wednesday, September 5, 2012

Inheritance


Inheritance:

Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.
When we talk about inheritance the most commonly used keyword would be extends and implements. These words would determine whether one object IS-A type of another. By using these keywords we can make one object acquire the properties of another object.

Example:

public class Animal{
}

public class Mammal extends Animal{
}

public class Reptile extends Animal{
}


public class Dog extends Mammal{
   public static void main(String args[]){

      Animal a = new Animal();
      Mammal m = new Mammal();
      Dog d = new Dog();

      System.out.println(m instanceof Animal);
      System.out.println(d instanceof Mammal);
      System.out.println(d instanceof Animal);
   }
}


In Object Oriented terms following are true:
  • Animal is the superclass of Mammal class.
  • Animal is the superclass of Reptile class.
  • Mammal and Reptile are sub classes of Animal class.
  • Dog is the subclass of both Mammal and Animal classes.