XFireServlet
org.codehaus.xfire.transport.http.XFireConfigurableServlet
0
XFireServlet
/servlet/XFireServlet/*
XFireServlet
/services/*
index.jsp
package com.test.ws;
import java.util.List;
/**
* Title: IHelloService
* Description: 定义接口
*/
public interface IHelloService {
public String getTestString(String str);
public ExceedVO getTestObject(EnterVO vo);
public List getTestList(List list);
}
package com.test.ws;
import java.util.ArrayList;
import java.util.List;
/**
* Title: HelloServiceImpl
* Description: 接口实现
*/
public class HelloServiceImpl implements IHelloService {
@Override
public String getTestString(String str) {
return "Hello" + str;
}
@Override
public ExceedVO getTestObject(EnterVO vo) {
ExceedVO revo = new ExceedVO();
revo.setDescStr("名称:"+vo.getName()+"年龄:"+vo.getAge());
return revo;
}
@Override
public List getTestList(List list) {
List users = new ArrayList();
for(int i=0; i<9; i++){
EnterVO user = new EnterVO("姓名"+i, i);
users.add(user);
}
return users;
}
}
package com.test.ws;
import java.io.Serializable;
/**
* Title: EnterVO
* Description: 入参对象
*/
public class EnterVO implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public EnterVO(String name, int age) {
this.name = name;
this.age = age;
}
public EnterVO() { }
}
package com.test.ws;
import java.io.Serializable;
/**
* Title: ExceedVO
* Description: 出参对象
*/
public class ExceedVO implements Serializable {
private static final long serialVersionUID = 1L;
private String descStr;
public String getDescStr() {
return descStr;
}
public void setDescStr(String descStr) {
this.descStr = descStr;
}
}
HelloService
http://test/HelloService
com.test.ws.IHelloService
com.test.ws.HelloServiceImpl
xsi:schemaLocation="http://xfire.codehaus.org/schemas/1.0/mapping.xsd">
package com.test.ws;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.xfire.XFire;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
/**
* Title: ClientTestServlet
* Description: 测试Servlet
*/
public class ClientTestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
Service serviceModel = new ObjectServiceFactory().create(IHelloService.class);
XFire xfire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xfire);
String serviceUrl = "http://localhost:8080/XFireWebService/services/HelloService";
IHelloService client = null;
try {
client = (IHelloService) factory.create(serviceModel, serviceUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
String str = client.getTestString("Beijing");
request.setAttribute("str", str);
EnterVO enterVO = new EnterVO();
enterVO.setName("Yaodi");
enterVO.setAge(26);
ExceedVO vo = client.getTestObject(enterVO);
request.setAttribute("vo", vo);
List al = new ArrayList();
al.add("111");
al.add("222");
al.add("333");
List list = client.getTestList(al);
request.setAttribute("list", list);
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
ClientTestServlet
com.test.ws.ClientTestServlet
ClientTestServlet
/ClientTest.do
ClientTest.do
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
测试客户端
测试客户端!!!
String:${str}
Object:${vo.descStr}
List:
${list.name} --- ${list.age}
package HelloWorld;
/**
* Title: HelloWorldService
* Description: 服务端
*/
public class HelloWorldService {
public String getMessage(){
return "Hello World!!!";
}
public String getMessage(String str1, String str2) {
return "Hello World! " + str1 + "&" + str2;
}
}
package HelloWorld;
import java.io.IOException;
import java.net.URL;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.soap.Constants;
import org.apache.soap.Fault;
import org.apache.soap.SOAPException;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;
import org.apache.soap.rpc.Response;
/**
* Title: HelloWorldClientServlet
* Description: 测试Servlet
*/
public class HelloWorldClientServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String endPoint = "http://localhost:8080/soap/servlet/rpcrouter"; //url是固定的,带参数绑定
Call call = new Call(); //创建一个RPC Call
call.setTargetObjectURI("hello"); //远程的服务名
call.setMethodName("getMessage"); //访问方法
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC); //设置编码风格
//发布带参数的服务
Vector params = new Vector();
Parameter p1 = new Parameter("name", String.class, "关羽", null);
Parameter p2 = new Parameter("name2", String.class, "张飞", null);
params.addElement(p1);
params.addElement(p2);
call.setParams(params);
URL url = new URL(endPoint); //SOAP服务的网址,开始发送RPC请求,并返回服务端的应答
Response resp = null;
try { //检查应答报中文是否有错(有错就打印出错信息,没错就打印返回信息)
resp = call.invoke(url, "");
} catch (SOAPException e) {
e.printStackTrace();
}
if(resp.generatedFault()){
Fault fault = resp.getFault();
System.out.println("The Following Error Occured:");
System.out.println("Fault Code=" + fault.getFaultCode());
System.out.println("Fault String=" + fault.getFaultString());
} else {
Parameter result = resp.getReturnValue();
System.out.println(result.getValue());
request.setAttribute("str", result.getValue());
}
request.getRequestDispatcher("/result.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
HelloWorldClientServlet
HelloWorld.HelloWorldClientServlet
HelloWorldClientServlet
/HelloWorldClientServlet.do
HelloWorldClientServlet.do
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page isELIgnored="false"%>
SOAP测试客户端
SOAP测试客户端!!!
String:${str}