axis与xfire开发

(1)使用axis开发webserivice
 1>首先到http://ws.apache.org/axis/上去下载最新的axis版本。最新版本是axis-1.4的。下载之后观察其目录,lib下就是所要用到的所有jar包了。这里少2个jar包,
 分别是activation.jar和mail.jar,需要单独下载。
 2>创建一个动态的web工程,修改下web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
		<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
			<display-name>
			websercice</display-name>
			
		<servlet>   
		    <servlet-name>AxisServlet</servlet-name>   
		    <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>   
		  </servlet>   
		     
		  <servlet-mapping>   
		    <servlet-name>AxisServlet</servlet-name>   
		    <url-pattern>/servlet/AxisServlet</url-pattern>   
		  </servlet-mapping>   
		     
		  <servlet-mapping>   
		    <servlet-name>AxisServlet</servlet-name>   
		    <url-pattern>*.jws</url-pattern>   
		  </servlet-mapping>   
		     
		  <servlet-mapping>   
		    <servlet-name>AxisServlet</servlet-name>   
		    <url-pattern>/services/*</url-pattern>   
		  </servlet-mapping>   
		</web-app>  

 3>开发web服务
  *接口开发

                     package com.mypack;

		public interface Hello {
		    public String sayHello(String name);

		}

  *接口实现类开发

                     package com.mypack;

		public class HelloService implements Hello {

		    public String sayHello(String name) {
			return "hello " + name + " !";
		    }

		}

 4>发布web服务,配置wsdd文件server-config.wsdd,将该文件放置在WEB-INF目录下

<?xml version="1.0" encoding="UTF-8"?>   
		<deployment xmlns="http://xml.apache.org/axis/wsdd/"  
		    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">   
		  <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>   
		     
		  <service name="hello" provider="java:RPC">   
		    <parameter name="className" value="com.mypack.HelloService"/>   
		    <parameter name="allowedMethods" value="sayHello"/>   
		  </service>   
		     
		  <transport name="http">   
		    <requestFlow>   
			<handler type="URLMapper"/>   
		    </requestFlow>   
		  </transport>   
		</deployment> 

 5>解压axis-1.4,将lib下的jar文件及mail.jar,activation.jar一并放置在WEB-INF下的lib目录中
 6>在TOMCAT服务器中,在webapps目录下创建my_axis_webservice目录,将工程下的WEB-INF拷贝到该目录下
   或者直接通过myeclipse发布也可
 7>启动TOMCAT服务器,访问http://localhost:8080/my_axis_webservice/services,查看是否有hello这个服务
 8>开发客户端
   在web工程下的web服务中(注意需要把axis.jar在eclipse加入到环境变量中),新建:

import java.net.MalformedURLException;
		import java.net.URL;
		import java.rmi.RemoteException;

		import javax.xml.rpc.ServiceException;

		import org.apache.axis.client.Call;
		import org.apache.axis.client.Service;

		public class Test {

		    /**
		     * @param args
		     * @throws ServiceException
		     * @throws MalformedURLException
		     * @throws RemoteException
		     */
		    public static void main(String[] args) throws ServiceException, MalformedURLException, RemoteException {

			// 标识WebService的具体路径
			String endpoint = "http://localhost:8080/my_axis_webservice/services/hello";
			// 创建Service实例
			Service service = new Service();
			// 通过Service实例创建Call实例
			Call call = (Call) service.createCall();
			// 将WebService的服务路径加入到Call实例中,并为Call设置服务的位置
			URL url = new URL(endpoint);
			call.setTargetEndpointAddress(url);
			// 调用WebService方法
			call.setOperationName("sayHello");
			// 调用WebService传入参数
			String res = (String) call.invoke(new Object[] { "mofeichen" });
			System.out.println(res);

		    }

		}

 (2)使用xFires开发webserivice
 1>下载xfire-distribution-1.2.6.zip包
 2>创建一个动态的web(xfire_web)工程,修改下web.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
		<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
			<display-name>
			xfire_web</display-name>
			<servlet>   
			<servlet-name>webservice</servlet-name>   
			<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>   
		    </servlet>   
		   
		    <servlet-mapping>   
			<servlet-name>webservice</servlet-name>   
			<url-pattern>/servlet/XFireServlet/*</url-pattern>   
		    </servlet-mapping>   
		    <servlet-mapping>   
			<servlet-name>webservice</servlet-name>   
			<url-pattern>/services/*</url-pattern>   
		</servlet-mapping>  
		</web-app>

 3>开发web服务
  *接口开发

                      package com.mypack;

	                 public interface Hello {
		    public String sayHello(String name);

		}

 *接口实现类开发

                     package com.mypack;

		public class HelloService implements Hello {

		    public String sayHello(String name) {
			return "hello " + name + " !";
		    }

		}

 4>发布web服务,配置wsdd文件services.xml(名称不能修改),将该文件放置在WEB-INF/classes/META-INF/xfire目录下

<beans  xmlns="http://xfire.codehaus.org/config/1.0">   
		 <service >   
		    <name>helloWorld</name>   
		    <namespace>http://localhost:8080/xfire_web/services/helloWorld</namespace>   
		    <serviceClass>com.mypack.Hello</serviceClass>   
		    <implementationClass>com.mypack.HelloService</implementationClass>   
		  </service>   
		</beans> 

 5>解压xfire-distribution-1.2.6.zip,将lib下的jar文件放置在WEB-INF下的lib目录中
 6>在TOMCAT服务器中,在webapps目录下创建xfire_web目录,将工程下的WEB-INF拷贝到该目录下
   或者直接通过myeclipse发布也可
   目录结构为(不要有变化,否则会找不到services.xml文件):
   --xfire_web
   |        |
   |        |
   |        ----WEB-INF
                      |
                      |
                      |---web.xml
                      |---lib
                      |---classes
                               |
                               |
                               |---放置.class的文件

                               |---META-INF
                                       |---xfire
                                              |
                                              |
                                               ---services.xml
 7>启动TOMCAT服务器,访问http://localhost:8080/xfire_web/services,查看是否有helloWorld这个服务
 8>开发客户端
   新建java工程,然后使用eclipse工具在工程上根据该服务的wsdl(即http://localhost:8080/xfire_web/services/helloWorld?wsdl)创建webservice client

import java.rmi.RemoteException;

		import javax.xml.rpc.ServiceException;

		import localhost.my_axis_webservice.services.helloWorld.HelloWorldLocator;
		import localhost.my_axis_webservice.services.helloWorld.HelloWorldPortType;

		public class Test {

		    /**
		     * @param args
		     * @throws ServiceException
		     * @throws RemoteException
		     */
		    public static void main(String[] args) throws ServiceException, RemoteException {
			
			HelloWorldLocator hello = new HelloWorldLocator();
			HelloWorldPortType p = hello.gethelloWorldHttpPort();
			String res = p.sayHello("saaaaaaaa");
			System.out.println(res);

		    }

		}

 也可以通过编写build.xml来生成客户端:

<?xml version="1.0" encoding="UTF-8"?>

<project name="facet" basedir="." default="create_code">

	<property name="optimize" value="false" />
	<property name="debug" value="on" />
	<property name="desprecation" value="false" />

	<property name="build.lib" value="${basedir}/lib">
	</property>
	<property name="source" value="${basedir}/src">
	</property>
	<property name="build.classes" value="${basedir}/build">
	</property>

	<path id="classpath">
		<pathelement location="${build.classes}" />
		<fileset dir="${build.lib}">
			<include name="*.jar" />
		</fileset>
	</path>

	<taskdef name="wsgen" classname="org.codehaus.xfire.gen.WsGenTask" classpathref="classpath">
	</taskdef>

	<target name="help" description="显示帮助信息">
		<echo message="target     描述">
		</echo>
		<echo message="-------------">
		</echo>
		<echo message="compile     编译">
		</echo>
		<echo message="create_code     生成代码">
		</echo>
	</target>

	<target name="compile" description="编译">
		<echo>编译程序代码</echo>
		<javac classpathref="classpath" destdir="${build.classes}" srcdir="${source}" debug="${debug}" optimize="${optimize}" deprecation="${desprecation}">
		</javac>
	</target>
	
	<target name="create_code" depends="compile" description="创建代码">
		<echo>创建代码</echo>
		<wsgen outputDirectory="${source}" wsdl="http://localhost:9999/xfire_web/services/helloWorld?wsdl" package="com.googlepages.smallnest.facet" overwrite="true"/>
	</target>


</project>

 

你可能感兴趣的:(tomcat,xml,Web,webservice,servlet)