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.