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.

SIMPLE CHAINED LIST DEMO IN JSF2

SIMPLE CHAINED LIST DEMO IN JSF2


   Today, I developed a simple lesson for chained list. I am  having combo1 showing the names of two states (tamilnad and kerala).
The user selects the desired state from the combo and clicks button1. The important cities from that state are displayed in list1.
This is the first step for ajaxified example to be developed. The facelet code is given below.
  I always preferred to name the facelet file as view.xhtml.


//view.xhtml

<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="cyan">

<h:form>

SELECT THE STATE, YOU  WILL GET THE CITIES.<br/>

        <h:selectOneMenu  id="list1"  value="#{cityguide.state}">


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

       </h:selectOneMenu>
<h:commandButton   id='button1'  value="show cities"
          action="#{cityguide.show}"  />


 <h:selectOneListbox value="#{cityguide.city}" id="list2">

        <f:selectItems value="#{cityguide.cities}"/>
 </h:selectOneListbox>


</h:form>

</body>

</html>




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

cityguide.java is the corresponding backing bean. as given below,



//cityguide.java


package  mypack;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.*;
import javax.faces.event.*;
import  java.util.*;
@ManagedBean(name="cityguide")
@SessionScoped




public class cityguide   implements  java.io.Serializable
{
String    state;
String    city;
Vector    cities;




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


        public  void    setCity(String   a){city=a;}
        public  String  getCity()      {return city;}




        public  void    setCities(Vector   v){cities=v;}
        public  Vector  getCities() { return  cities;}


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


        public  String   show()
         {
          String   a = getState();


          Vector   vector1 = new Vector();


          if(a.equals("tamilnad"))
          {
             vector1.removeAllElements();
             vector1.add("Chennai");
             vector1.add("trichy");
             vector1.add("kovai");
             vector1.add("madurai");
          }




          if(a.equals("kerala"))
          {
             vector1.removeAllElements();
             vector1.add("quilon");
             vector1.add("kochin");
             vector1.add("trivandrum");
             vector1.add(" trissur");
          }
          System.out.println(""+vector1);
          setCities(vector1);
          return  "";


       }                       
 }


       Then type the url as below
"http://localhost:8080/jsf2chainedapp/view.jsf"




 In the next post I will use AJAX for the same purpose. This example gives dynamic loading of listbox. There is no iteration code. I have simply bound a collection to list1.






Thursday 29 March 2012

JSF 1.1 CLIENT (TOMCAT5) FOR AXIS 1.1 DROP IN DEPLOYMENT WEBSERVICE

JSF 1.1 CLIENT (TOMCAT5)  FOR AXIS 1.1 DROP IN DEPLOYMENT WEBSERVICE


    Today, I developed a simple XML webservice by Drop in deployment method using axis 1.1.  This method is the easiest method to create webservice. All that we have to do is  to create a simple functionbean. We need not even compile it. Just place that java file in axis folder in tomcat5\webapps the current version of axis is AXIS2, but i am using AXIS1.1. Actually, I wanted to try connecting to the service from JSF 2. But JSF2 does not work in tomcat5. And axis does not work in tomcat6. Therefore I decided to try that in tomcat5. Both AXIS 1.1 and JSF1.1 will work in tomcat5. My function bean is named tourguide.java . I placed this file in tomcat5 webapps\axis as tourguide.jws.


     I started tomcat5. In the browser I typed  the URL as
  "http://localhost:8080/axis/tourguide.jws?wsdl"
         I found that wsdl was available.
----------------------------------------------------------------------
//tourguide.java
  
import java.util.*;


public   class tourguide
{
    public    String    showcities(String   s)
    {
       Vector    vector1 = new Vector();


       if(s.equals("kerala"))
       {
          vector1.addElement("quilon");
          vector1.addElement("tvm");
          vector1.addElement("cochin");
          vector1.addElement("kottayam");
       }




       if(s.equals("tamilnad"))
       {
         vector1.addElement("madurai");
         vector1.addElement("trichy");
         vector1.addElement("kovai");
         vector1.addElement("chennai");
       }




       if(s.equals("karnataka"))
       {
         vector1.addElement("bangalore");
         vector1.addElement("mangalore");
         vector1.addElement("mysore");
         vector1.addElement("hubli");
       }


          int   n = vector1.size();


          String   r = "";


          for( int j=0; j<n; j++)
          {
           String   v = (String)  vector1.elementAt(j);
           r = r+v+"\n";
          }


          return   r;
     }
}
**********************************************************************
  Then I created a java client program for the above webservice as shown below.

//tourjwsclient.java

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    tourjwsclient
{


   public static void main(String args[])
   {
     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 = args[0];

  //   String s2=(String)call.invoke(new Object[]{r});  // important!

     String    vector  = (String)call.invoke(new Object[]{r});   
     System.out.println(vector);                   

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

   }

}
***********************************************
To compile this, I placed  the following jar files in classpath.

set 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 it successfully. In the above program note the URL . We have already started tomcat5. Therefore webservice is available. We can connect to this webservice from our console program. My working folder is G:\axisnew.

g:\axisnew>java  tourjwsclient  kerala

       I  got the following result.

**********************************************
 Now, I converted this main program into a functionbean(helper class). I named it as tourjwshelper.java  shown below.
(As I am going to use it in jsf backing bean, I added the package statement)

//    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;

  //   String s2=(String)call.invoke(new Object[]{r});  // important!

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




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

          return   v;

   }

}
**************************************
 Now I create two files for jsf 1.1 (input.jsp and backer.java).

input.jsp
----------
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN">

 <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

 <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

 <f:view>
 <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>
 </f:view>
**********************************************
  I created a new context in tomcat5 webapps as axistourjsfapp.
I placed the above jsp file in the root of this context. i created WEB-INF under the root. Placed web.xml and faces-config.xml inside WEB-INF

web.xml
----------
<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

    <context-param>
     <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
      <param-value>client</param-value>
    </context-param>

    <context-param>
    <param-name>com.sun.faces.validateXml
    </param-name>
    <param-value>true</param-value>

    </context-param>

    <context-param>
        <param-name>com.sun.faces.verifyObjects</param-name>
        <param-value>true</param-value>

    </context-param>

    <!-- Faces Servlet -->
    <servlet>
  <servlet-name>Faces Servlet </servlet-name>
   <servlet-class>javax.faces.webapp.FacesServlet
   </servlet-class>

   <load-on-startup> 1 </load-on-startup>
    </servlet>


    <!-- Faces Servlet Mapping -->
    <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
     <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>


</web-app>
*******************************************
faces-config.xml
------------------------
<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE faces-config PUBLIC
  "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
  "http://java.sun.com/dtd/web-facesconfig_1_1.dtd">

<faces-config>

  <application>
    <locale-config>
      <default-locale>en</default-locale>

    </locale-config>
  </application>

      <managed-bean>

         <managed-bean-name>backer</managed-bean-name>

         <managed-bean-class>mypack.backer
        </managed-bean-class>
         <managed-bean-scope>session</managed-bean-scope>
             </managed-bean>

    </faces-config>
****************************************

Now I created classes folder and lib folder under WEB-INF. I created mypack folder under classes folder. I placed the tourjwshelper.class in mypack folder. I placed the following jar files in lib folder

Now comes the code for backing bean

//backer.java
package  mypack;

import javax.faces.context.*;
import javax.faces.component.*;
import javax.faces.validator.*;



public class backer
{
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   void   show()
   {
        tourjwshelper   bean1  =  new tourjwshelper();
        String r=bean1.getcities(state);

        setResult(r);
   }
}
**********************************************
shutdown tomcat5 because we have  made changes in WEB-INF
and restart. I typed the URL as 
  "http://localhost:8080/axistourjsfapp/faces/input.jsp"

I selected 'tamilnad' from the combo and clicked show  button. I got the result in area1. Therefore, I am now happy that I am able to connect to a web service from my JSF1.1 program.
    I will have to try to get similar result in JSF2.
***************************************
 Web service is slow. So i did not get result for some time. And then I got it.