Friday 6 April 2012

TECHNOLOGY VARIANTS

TECHNOLOGY  VARIANTS



       In the matter of technology, we have very wide choices with a lot of  combinations. 


Any modern web application, should be based on MVC architecture. There are mainly two frameworks. namely STRUTS and JSF.
     STRUTS is a page-centric technology. JSF is component-centric technology. JSF is a JCP standard. 
   STRUTS has two variants. STUTS1.1 and STRUTS2. 
Similarly JSF also has two variants(i.e)JSF1.1 and JSF2.




************************************************
Next comes the database. Oracle of course is the industry standard. But  it is not free.More over, many web projects like mine, do not need the advanced features available in oracle. So I am using 
MySql 5 Database. It is open source and free.
*************************************************
Next decision to be made is about access technology. There are mainly two approaches. 
   1)JDBC
   2)ORM(Object Relational Mapping)
                 a)Hibernate
              b)JPA(JCP standard)

***************************************************
Nowadays,   Dependency Injection  ( ) is essential. It enables 'Declarative Change Management' . Spring is the standard DI container. In a number of projects spring is an essential container.
Therefore, I am integrating Struts1.1 with spring.
  Spring provides special templates for JDBC, Hibernate and JPA.
Therefore, I am using two combinations.
                     a)Spring ,    Struts1.1,          JDBC template
                     b)Spring ,    Struts1.1,         Hibernate Template
Note:
    Unlike Struts1.1,  JSF1.1,  JSF2 and Struts2 have built-in support for dependency injection.
JSF 2 and Struts 2 can make use of annotation which makes xml configuration file unnecessary. Therefore I have made use of JSF2 with annotation. 



******************************************************
So far, We are assuming that every thing is done in web tier only. But In modern days, SOA(Service Oriented Architecture) is very important. A simpler word is XML web service(making use of SOAP, WSDL and UDDI). AXIS 1.1 and AXIS 2  are opensource and free webservice technologies from Apache Software Foundation(ASF). AXIS 2 can make use of annotation.

  In both AXIS1.1 and AXIS2, We can use either Drop-in-deployment or WSDD deployment. I am using both in AXIS 1.1.
************************************************
We have to provide mobile client. The mobile  device may be either a J2ME device or Android device. Therefore, I have provided these two clients.

******************************************************
Finally, When the data and number of clients become extremely  large, we will deploy it in J2EE container. A J2EE container provides services like JMS, JNDI, EJB and JavaMail. The important point is that it gives transaction support. JBOSS4 and JBOSS5 are opensource and free J2EE containers. As a bonus we get JBOSS portal. Therefore I aim to deploy my application in JBOSS4/5.

******************************************************
 The easiest way and standard way for developing web applications is using command-line for compiling and deploying.
Consider the syllabus for Sun certified web component developer exam(now known as OCWCD). There is no mention about any IDE  or any web container except tomcat there. Tomcat is the reference implementation.

However, Many companies are using Eclipse. Netbeans is less popular. Therefore I have done my work first in command-line and tomcat, and then  in tomcat within Eclipse3.6(Ganymede)
****************************************************











   
    
   

Sunday 1 April 2012

ABOUT JAVA SERVER PROGRAMMING JAVA EE6 -BLACK BOOK

ABOUT  JAVA SERVER PROGRAMMING 
        JAVA EE6 -BLACK BOOK
  (BY KOGENT LEARNING SOLUTIONS )
 published by Dreamtech(priced at 600/- with CD)
*******************************************


              In this post I want to share information about the above very nice book with students and developers who are working in J2EE field. This book is very special because it introduces and covers almost all the important topics that are essential for J2EE developers today. It gives latest developments. I simply give the chapter headings. Readers can judge for them selves.


      Java EE6 : An overview
      Web Applications and Java EE 6
      Working with JDBC 4.0
      Working with Servlets
      Handling sessions in servlets 3.0
     Implementing Jsp Tag Extensions
     Implementing JSTL 
     Implementing Filters
     Working with JSF 2.0
     Understanding JavaMail 1.4
     Working with EJB 3.1
     Implementing Entities and JPA 2.0
     Implementing Java persistence with Hibernate 3.5
     Implementing JBOSS Seam
     Java Connector Architecture 1.6 
     Java EE Design patterns
     Implementing SOA using java Webservices
    Working with STRUTS2
    Working with Spring 3.0
    Securing Java EE 6 Applications


   People Management Solutions(project with full code and   
   explanations)


    Appendix 
                 A: AJAX
                 B: Installing Java EE 6 SDK
                 C:Working with Netbeans IDE 6.8
                 D: Implementing Internationalization(I18N)
                 E:Working with Facelets
                 F:Working with JMS
*************************************************
   It is my aim to study all these chapters in the coming months.


    

CRUD OPERATION AS AXIS 1.1 WEBSERVICE WITH JSF2 CLIENT

CRUD OPERATION  AS AXIS 1.1 WEBSERVICE 
               WITH JSF 2 CLIENT


***************************************************


  Nowadays, it is very important to do everything as XML webservice. That is because, a java webservice can be used not only by java clients but by other clients like DOT NET platform and PHP. It can also  be used by J2ME client. I have already written about axis webservice used by J2ME client. In this demo I want to show how we can carryout CRUD operations using webservice and JSF-2 client . There are two methods by which we can create webservice by using AXIS 1.1.


                      a) Drop-in-deployment
  
            It means we simply write a java function bean and place the source file in axis folder of tomcat6\webapps(It is assumed that we have already placed axis1.1 application context folder in tomcat6\webapps ). Then we can type the URL as 
          "http://localhost:8080/axis/hellobean.jws?wsdl"
  so, the service is published. This is very easy method. 
---------------------------------------------------------------
               b)WSDD method of deployment


But what to do if we do not have the source file and we have been given a class file for the function by somebody else. That is why the second method is given. It is known as WSDD(WebService Deployment Descriptor) method. It is that method that I am going to follow now. Therefore I will begin with dbeditorbean.java as shown below. My working folder is g:\axisnew.
**********************************************
(I am following a good book on 'AXIS(Next generation java SOAP)'-Romin Irani, S.Jeelani Bhasha published by WROX press)



import java.sql.*;

public  class   dbeditorbean
{
     public   String     edit(String  a, String b, String c, String d)
     {
     String   r="";

     try
     {
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     String     url = "jdbc:odbc:contacts";
     Connection connection = DriverManager.getConnection(url);
    
     String     sql ="";

     if(d.equals("add"))
 { sql="insert into table1 values ('"+a+"', '"+b+"', '"+c+"') "; }

     if(d.equals("delete"))
 {sql=" delete   from table1 where key='"+a+"'   ";}

      if(d.equals("update"))
 {sql="update table1 set table1.name='"+b+"', table1.place='"+c+"'  where table1.key='"+a+"'  "; }

      if(d.equals("verify"))
 {  sql ="select * from table1 where key='"+a+"'  "  ; }

System.out.println(sql);
      //--------------------------------------------------
  //   Statement   statement;
  Statement      statement   = connection.createStatement();
 
      if(d.equals("verify"))
      {

      ResultSet     rs  = statement.executeQuery(sql);
      int   n=0;
        while(rs.next())
        {
        r= r+rs.getString(1)+ "\n"+
              rs.getString(2)+ "\n"+
              rs.getString(3)+"\n"+
              "--------"+"\n";
        n = n+1;
        }
     if(n==0) {  r="no such record";   }
      }
      //---------
      else
      {
       statement.execute(sql);
      r="done! please verify";
       connection.close();    //  very important
     }


     }   catch(Exception e1) { System.out.println(""+e1);}

         return   r;

     }
}
===================================================

I compiled it .

Then, I copied the class file to:
d:\tomcat6\webapps\axis\WEB-INF\classes

==================================================
In the next step, I created the wsdd file.
( web service deployment descriptor).

//   dbeditorbean.wsdd

<deployment
       xmlns="http://xml.apache.org/axis/wsdd/"
       xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">


 <service     name="dbeditservice"      provider="java:RPC">
    <parameter  name="className"  value="dbeditorbean"   />
    <parameter  name="allowedMethods" value="*"       />
 </service>


</deployment>

=================================================

  Then I started tomcat6
how?
d:\tomcat6\bin>tomcat6
-----
Then I gave the command as follows.

g:\axisnew>java  org.apache.axis.client.AdminClient    (space) dbeditorbean.wsdd (space)
           -lhttp://localhost:8080/axis/services/AdminService



( this should be typed in a single line).
===
I got the response that
Processing file dbeditorbean.wsdd
<Admin>Done processing</Admin>

============================

Now, I wanted to test if the service is working.
So, I wrote the following client program.

// dbeditwsddclient.java

import java.net.*;
import java.io.*;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;


class   dbeditwsddclient
{

   public   static   void   main(String[]   args)
   {
   
  
     try
     {                     
     System.out.println("Start");


     Service service    =    new Service();
     System.out.println("Service ready");

     Call      call=(Call)service.createCall();
     System.out.println("call ready");

     URL url=
       new URL("http://localhost:8080/axis/services/dbeditservice");

       System.out.println("url   ok");


    call.setTargetEndpointAddress(url);                              
     call.setOperationName( new QName("dbeditorbean","edit"));

     String   a = args[0];
     String   b = args[1];
     String   c = args[2];
     String   d = args[3];



     call.addParameter("a",XMLType.XSD_STRING,ParameterMode.IN);
     call.addParameter("b",XMLType.XSD_STRING,ParameterMode.IN);
     call.addParameter("c",XMLType.XSD_STRING,ParameterMode.IN);
     call.addParameter("d",XMLType.XSD_STRING,ParameterMode.IN);

     call.setReturnType(XMLType.XSD_STRING);



     String r=(String)call.invoke(new Object[]{a,b,c,d});  // important!


     System.out.println(r);          

     }catch(Exception e){System.out.println(""+e);}

   }

}
=======================================================
  I have already set the path and classpath.
as follows.
PATH=c :\windows\command;c:\jdk1.5\bin
CLASSPATH=g:\axisnew;
  e:\axis11\lib\axis.jar;

  e:\axis11\lib\commons-discovery.jar;

  e:\axis11\lib\commons-logging.jar;

  e:\axis11\lib\jaxrpc.jar;

  e:\axis11\lib\saaj.jar;

  e:\axis11\lib\log4j-1.2.8.jar;

  e:\axis11\lib\xercesImpl.jar
=================================
Now, I compiled the client program.
Then I ran the program.
I got response as shown below.

G:\axisnew>java dbeditwsddclient   "101"  "  "  "  "  verify
Start
Service ready
call ready
url   ok
101
SARAH
KARAIKKUDI
--------
So, I am sure that the  service is working.

In the next step, I converted the client program into a
component.
Just changed main into dbedit.
----------------------------------------------------------------------------

 So that I can use it in JSF 2 application.

backer.java
--------------
package  mypack;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.*;
import javax.faces.event.*;
import  java.util.*;
@ManagedBean
@SessionScoped



public class backer implements  java.io.Serializable
{


String    key;
String    name;
String    place;
String    operation;

String     result;

 public   void    setKey(String a)   { key=a;}
 public   void    setName(String a)   { name=a;}
 public   void    setPlace(String a)   { place=a;}
 public   void    setOperation(String a)   { operation=a;}

 public   void    setResult(String a)   { result=a;}



   public   String  getKey()   { return  key ; }
   public   String  getName()   { return  name ; }
   public   String  getPlace()   { return   place; }
   public   String  getOperation()   { return   operation; }

 public   String  getResult()   { return   result; }

    public   String   show()
   {
        String a=getKey();
        String b=getName();
        String c=getPlace();
        String d=getOperation();

        dbeditwsddclientbean   bean1  =  new dbeditwsddclientbean();
       
        String r=bean1.dbedit(a,b,c,d);

        setResult(r);
       return null;
   }
}
*************************************************************************
view.xhtml
*************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
            xmlns:h="http://java.sun.com/jsf/html">
           

 <body          bgcolor="cyan">
   <h:form id="form1" >
      <h:inputText id="text1" value="#{backer.key}" /> key:<br/>
      <h:inputText id="text2" value="#{backer.name}" /> name:<br/>
      <h:inputText id="text3" value="#{backer.place}" /> place:<br/>

       <h:selectOneMenu  id="operation"  value="#{backer.operation}">

          <f:selectItem  itemLabel="add" itemValue="add"/>
          <f:selectItem  itemLabel="delete" itemValue="delete"/>
          <f:selectItem  itemLabel="update" itemValue="update"/>
          <f:selectItem  itemLabel="verify" itemValue="verify"/>


       </h:selectOneMenu>

       <br />
       <h:commandButton   id="button1"
                          action="#{backer.show}"
                          value="show" />
                          <br />

  <h:inputTextarea rows="20" cols="40" id="text4"   value="#{backer.result}" />

   </h:form>
  </body>
  </html>

Then copy the jar files in classpath (listed above ) to the Tomcat6\webapps\axiswsddjsf\WEB-INF\lib.
Compile backer.java and copy it to Tomcat6\webapps\axiswsddjsf\WEB-INF\classes\mypack  file
 and  view.xhtml   to  Tomcat6\webapps\axiswsddjsf
  then I typed the url as "http://localhost:8080/axiswsddjsf/view.jsf"


I just tested for verify operation for the key 101. I got the result. Other operations will also work.





                            













Saturday 31 March 2012

JSF2 CLIENT FOR AXIS 1.1 WEBSERVICE

JSF2 CLIENT FOR AXIS 1.1 WEBSERVICE


         Yesterday I tried to create webservice by using axis 1.1 in tomcat6. But I was not able to. I thought that the reason could be older version of AXIS 1.1. I read in web that though axis 2 is the current version, there are number of projects which are using AXIS1.1. Therefore I downloaded the latest version of axis 1.1 from apache axis 1.1 site. that version is  axis-bin-1_4. When I unzipped it I got axis-1_4 folder. Inside the folder I had

Inside the webapps folder  I had axis folder. I copied that axis folder to d:\tomcat6\webapps folder. I found that drop in deployment worked correctly.  My jsf 2 application also working in the same tomcat6. I got correct result as what I got yesterday with tomcat5 and jsf1.1. AXIS 1.1 is not obsolete.
  screen  shot is given below


code is given below,

view.xhtml
--------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
            xmlns:h="http://java.sun.com/jsf/html">
            

 <body          bgcolor="cyan">
   <h:form id="form1" >

       <h:selectOneMenu  id="operation"  value="#{backer.state}">

          <f:selectItem  itemLabel="kerala" itemValue="kerala"/>
          <f:selectItem  itemLabel="tamilnad" itemValue="tamilnad"/>
          <f:selectItem  itemLabel="karnataka" itemValue="karnataka"/>


       </h:selectOneMenu>


       <h:commandButton   id="button1"
                          action="#{backer.show}"
                          value="show" />

  <h:inputTextarea rows="20" cols="40" id="text4"  value="#{backer.result}" />

   </h:form>
  </body>
  </html>
****************************************************
//backer.java
-------------
package  mypack;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.*;
import javax.faces.event.*;
import  java.util.*;
@ManagedBean
@SessionScoped



public class backer implements  java.io.Serializable
{
String     result;

String    state;



 public   void    setState(String a)   { state=a;}

 public   void    setResult(String a)   { result=a;}



 public   String  getState()   { return   state; }

 public   String  getResult()   { return   result; }

    public   String   show()
   {
        tourjwshelper   bean1  =  new tourjwshelper();
        String r=bean1.getcities(state);

        setResult(r);
       return null;
   }
}
**************************************************
//tourjwshelper.java
package  mypack;

import java.net.*;
import java.util.*;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Service;
import org.apache.axis.client.Call;
import org.apache.axis.encoding.XMLType;

public class    tourjwshelper
{


   public   String    getcities(String s)
   {

        String   v="";


     try
     {                      
     System.out.println("Start");
     String s1="showcities";

     Service     service =   new Service();
     System.out.println("Service ready");


     Call    call   =   (Call)   service.createCall();
     System.out.println("call ready");

     URL      url   =
           new URL("http://localhost:8080/axis/tourguide.jws");

     call.setTargetEndpointAddress(url);

     call.setOperationName(s1);
     call.addParameter("name",XMLType.XSD_STRING,ParameterMode.IN);
     call.setReturnType(XMLType.XSD_STRING);

     String   r = s;

         v  = (String)call.invoke(new Object[]{r});   


     }catch(Exception e){System.out.println(""+e);}

          return   v;

   }

}

and the .jws  file is same as given in webservice previous post.



















Friday 30 March 2012

SIMPLE VALIDATION DEMO IN JSF2

SIMPLE VALIDATION DEMO IN JSF2


   Validation is very important in any web framework. In struts 1.1 we have validation framework with readymade scripts for validation of email,credit card, phone number etc. It is very difficult to write our own regular expression for such validations.


   Similarly, Struts2 also has very good validation support. In JSF 2 it looks that we do not have much ready made validation support. I may be wrong. I find there are three standard JSF validators  tags  in JSF 2. 

LengthValidator
<f:validateLength>
maximum, minimum
DoubleRangeValidator
<f:validateDoubleRange>
maximum, minimum
LongRangeValidator
<f:validateLongRange>
maximum, minimum

    
    To learn how to use these tags I developed a simple demo. As usual I have written a view file(view.xhtml) and 2 java files (backer and helper bean).
         1.view.xhtml
         2.backer.java
         2.echobean.java

view.xhtml
-------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html">
 <body     bgcolor="green"   text="white">

  <f:view>
     <h:form id="form1">

            <h:outputText  value="name"/>
      <h:inputText id="name" value="#{backer.name}"  >
        <f:validateLength      minimum="3"      maximum="10"/>
      </h:inputText>
                 <h:message for="name"/>
       <br/>

            <h:outputText  value="place"/>

            <h:inputText id="place" value="#{backer.place}" required="true" />
            <h:message                  for="place"/>
            <br/>

<h:outputText  value="result"/>
     <h:inputText id="text3" value="#{backer.result}"   /><br/>

       <h:commandButton   id="button1"
                          action="#{backer.echo}"
                          value="click"/>
<br/>
     </h:form>
</f:view>

    </body>
</html>
********************************************************
Note the color code. I am using red color for the backing bean. Green color for backing bean attributes. rose color for backing bean method or function.
--------------------------------------------------------------------------------
<f:message> is placed out side but we indicate the control id to which it refers.

backer.java
--------------
package mypack;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.*;

@ManagedBean
@SessionScoped



public class backer 
{

String  name;
String place;
String result;



       public void setName(String a){   name=a;  }
       public void setPlace(String a){   place=a;  }
        public void setResult(String a){result=a;}


        public  String getName(){return name;}
        public  String getPlace(){return place;}
        public  String getResult(){return result;}

     //------------------------------------------

        public String   echo()
        {

        String s1=getName();
        String s2=getPlace();

        echobean  bean1 =  new  echobean();
        String r=bean1.echothis(s1,s2);

        setResult(r);
        return  "success";

        }

}




-----------------------------------------------------------------------
 In a future blog post I will explore more advanced validation.