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.