What is Java Server Faces and How to use it?

Java Server Faces is an MVC based framework to design User Interface for Server Side Technologies. We can use UI widgets in server based event handlers. The Java Servlet container is used to run these applications. JSF provides core libraries, UI components and many more extended features to create a well defined UI for Web server applications. In crux, JSF or Java Server Faces is a standard technology which is highly used in the Java Developers Community.

Java Server Faces Architecture

JSF architecture is based on the MVC (Model View Controller) architecture. UI Components can be rendered in many ways like Computing Device, HTML browser etc. The diagram for the JSF architecture is shown as follows.

Java Server Faces Architecture

Advantages of JSF

JSF is highly advantageous due to the following reasons:

  1. Presence of Reusable UI components
  2. JSF manages multiple server requests at a time
  3. Multiple Custom components can be used by implementing JSF
  4. Simple and Easy to maintain

Lifecycle of JSF

JSF lifecycle contains 6 phases as mentioned in the figure below.

Java Server Faces Lifecycle

Steps to create a simple JSF application:

The basic requirement is to install the archetype-webapp plugin. The step by step implementation in given below for Windows Operating System using Eclipse IDE.

  1. Open command prompt (cmd) to set the path for JSF application
  2. use the command: mvn archetype:create to initiate mavern
  3.  Add the JSF capability to the project. Also add all the JSF dependencies to the project.
  4. Open Eclipse IDE and create a new Java project
  5. Import the project created earlier using JSF capability using import button in Eclipse
  6. Configure the Faces Servlet in web.xml file of the project created in Eclipse
  7. Create a Managed Bean for the given package of the java program
  8. Create a JSF page under the web app directory in Eclipse IDE.
  9. Build the project and run it as an maven package
  10. Deploy WAR file, run the a wizard project and start the tomcat server.
  11. Run the complete application using the run command, which will launch the application on the server

Spring Framework for Web Technologies

Spring Framework is an open source application framework for java based platforms. It is used to build Web applications along with the use of Java Enterprise Edition (J2EE). It is very popular in Java Developer Community. It is highly lightweight and occupies only 2MB of storage space. It uses POJO (Plain Old Java Object) Development model to create faster J2EE applications.

Components of Spring Framework and its Architecture

Spring Framework architecture is a modular type of architecture. That means it is made up of multiple modules in the containers mentioned below:

  1. Test: It is the first and the lowest layer of Spring Architecture
  2. Core Container: It contains modules like Core, Beans, Context, and Regular Expressions etc.
  3. AOP (Aspect Oriented Programming), Aspects and Instrumentation
  4. Data Access integration: It contains modules like JDBC, ORM (Object Relational Mapping), OXM (Object XML Model), JMS (Java Messaging Service) and Transaction
  5. Web: This layer contains web connectivity and client-server connection modules like MVC, Web Socket Module, Web Portlet.

    Spring Framework Architecture

Advantages of Spring Framework

  1. Easy testing capabilities
  2. Lightweight and non invasive
  3. Fast Development Framework like JTA, JDBC, JMS etc. can be used along with Spring
  4. Powerful Abstraction implementation
  5. High validation and transaction support

Key features of Spring Framework

AOP (Aspect Oriented Programming)

AOP is a key element of this framework. In AOP the users are able to handle aspects like caching, logging, security, declarative transactions etc. AOP is used to conduct Aspect Oriented Implementation of Spring framework. Similar to a class in OOP (Object Oriented Programming), AOP is based on the concept of an Aspect.

Dependency Injection

DI or Dependency Injection keeps the Spring Framework application classes independent from other Java classes in the programming interface or API. We are able to integrate these independent classes by using DI. ‘D’ means Dependency which is an association of two classes. ‘I’ stands for Injection which is implementation of other classes with the help of the previous class.

Basic Steps to create a Application using Spring Framework

  1. Create a Java project in any IDE available like Eclipse IDE or NetBeans
  2. Install and add all the required Spring Framework Libraries that will be used in Spring Implementation
  3. Create Source Files for the Java program created in the 1st step
  4. Create Bean Configuration page inside the SRC directory to bind the classes together
  5. Run the program and deploy the application

How to compile and execute DLL using JNI in Java on Fedora?

Compile and Execute DLL using JNI in Java on Fedora is a common practice in cross platform programming. Java Native Interface, as the name indicates, is an interface between Java App code and other codes like C/C++ programs. After creating and compiling the Java file containing specific functionality, header files can be created by using JNI. These header files then are included in C/C++ program to run functions coded in Java.

Dynamic Link Library is commonly termed as DLL is a file type used to group instructions that are executed by other programs. A DLL file itself does nothing, it provide functionality to other programs. DLLs are very helpful in making efficient application since they get loaded in memory only when needed by the calling program. One DLL file can be shared by multiple programs thus improving resource utilization.

This is demonstrated with Java code to implement basic arithmetic operations. The cal1.Java is compiled and cal1.h file is created. It is then used in try1.c file.

Steps to compile and execute DLL using JNI in Java on Fedora

  1. Compile the java program written for DLL by using the following command on the terminal:

javac cal1.java

  1. Create the header file for the java program by using the following command:

javah -jni cal1

  1. Compile the C program written as a part of DLL execution by taking the reference from the Java header file.

Type the following command in the terminal. Follow the substeps to find out the paths of the JNI files.

gcc -I /usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79-2.5.5.0.fc20.x86_64/include/ -I/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.79-2.5.5.0.fc20.x86_64/include/linux/ -shared -o libcalc.so try1.c

  1. locate the jni.h file and copy its path at the place of path1 in the note below
  2. locate the jni_md.h file and copy its path at the place of  path2 in the note below
  3. Note for C file Execution: gcc path1 path2 -shared -o lib***. So nameOFC.c is the short term for the 3rd step.
  4. Run the java file to get the results on the terminal:
    java –Djava.library.path=`pwd` cal1

Note that here, pwd the path where the DLL file is located. cal1 is the name of the main class in the java file.

This way we are able to execute DLL program for a calculator in Fedora.

cal1.java
 
import java.util.*;
public class cal1
{
 static
  {
     System.loadLibrary("calc");
  } 
  
private native double addFunc(double a,double b);
private native double subFunc(double a,double b);
private native double mulFunc(double a,double b);
private native double divFunc(double a,double b);
 public static void main(String arg[]) throws Exception
{
  //cal c=new cal();
Scanner sc=new Scanner(System.in);
  
  int choice=1;
  double num1,num2;
        
        while(choice!=5)
      {  
                  System.out.println("\n Enter choice \n1.Add\n2.Subtract\n3.Multiply\n4.Division\n5.exit"); 
                  choice=sc.nextInt();      
                  
                  
                  System.out.println("\n Number 1 : "); 
                  num1=sc.nextDouble();      
                  
                  System.out.println("\n Number 2 :"); 
                  num2=sc.nextDouble();      
                  
                  switch(choice)
                  {      
                  
                        case 1:              
                               System.out.println("Sum of Two Numbers is ="+new cal1().addFunc(num1,num2)); 
                              break;
                        case 2:
                              System.out.println("Difference of Two Numbers is="+new cal1().subFunc(num1,num2));
                              break;                  
                        case 3:
                              System.out.println("Product of Two Numbers="+new cal1().mulFunc(num1,num2));
                              break;                  
                        case 4:
                              System.out.println("Division of Numbers="+new cal1().divFunc(num1,num2));
                              break;                  
                  }
      }
}
}
cal1.h
 
#include <jni.h>
/* Header for class cal1 */
#ifndef _Included_cal1
#define _Included_cal1
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     cal1
 * Method:    add
 * Signature: (DD)D
 */
JNIEXPORT jdouble JNICALL Java_cal1_addFunc
  (JNIEnv *, jobject, jdouble, jdouble);
/*
 * Class:     cal1
 * Method:    sub
 * Signature: (DD)D
 */
JNIEXPORT jdouble JNICALL Java_cal1_subFunc
  (JNIEnv *, jobject, jdouble, jdouble);
/*
 * Class:     cal1
 * Method:    mul
 * Signature: (DD)D
 */
JNIEXPORT jdouble JNICALL Java_cal1_mulFunc
  (JNIEnv *, jobject, jdouble, jdouble);
/*
 * Class:     cal1
 * Method:    div
 * Signature: (DD)D
 */
JNIEXPORT jdouble JNICALL Java_cal1_divFunc
  (JNIEnv *, jobject, jdouble, jdouble);
#ifdef __cplusplus
}
#endif
#endif
try1.c
 
  1. #include<jni.h>
  2. #include<stdio.h>
  3. #include"cal1.h"
  4. JNIEXPORT jdouble JNICALL Java_cal1_addFunc(JNIEnv *env, jobject obj, jdouble a, jdouble b)
  5. {
  6. return(a+b);
  7. }
  8. JNIEXPORT jdouble JNICALL Java_cal1_subFunc(JNIEnv *env, jobject obj, jdouble a, jdouble b)
  9. {
  10. return(a-b);
  11. }
  12. JNIEXPORT jdouble JNICALL Java_cal1_mulFunc(JNIEnv *env, jobject obj, jdouble a, jdouble b)
  13. {
  14.       return (a*b);
  15. }
  16. JNIEXPORT jdouble JNICALL Java_cal1_divFunc(JNIEnv *env, jobject obj, jdouble a, jdouble b)
  17. {
  18.       return (a/b);
  19. }
post

Database Recovery Techniques Available in DBMS

Why Database Recovery Techniques are important?

Database are prone to failures due to inconsistency, network failure, errors or any accidental damage. Reliable database recovery techniques are highly important to bring a database back into a working state after a failure. Recovery includes the procedures of restoring database to the earlier and previously existing condition. Read More