webservice接口的发布
使用xfire-client发布webservice接口:
commons-codec-1.3.jar
commons-httpclient-3.0.jar
在src 下创建META-INF/xfire/services.xml
=====================mail方法===============================
package cn.qgc.ch05.main;
import java.util.List;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import cn.qgc.ch05.web.WebInteface;
import cn.qgc.ch05.web.biz.WebUserBiz;
import cn.qgc.ch05.web.entity.User;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
//创建服务接口模板
Service service=new ObjectServiceFactory().create(WebInteface.class);
//代理工厂
XFireProxyFactory factory=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String Url="http://localhost:8080/web/services/CommonService";//web地址
try {
WebInteface web=(WebInteface)factory.create(service,Url);//根据接口和地址返回服务类
System.out.println(web.wether());//调用天气预报的接口
//注册用户
//创建服务接口模板
Service serviceUser=new ObjectServiceFactory().create(WebUserBiz.class);
//代理工厂
WebUserBiz sser=(WebUserBiz)factory.create(serviceUser,Url);//根据接口和地址返回服务类
User users=new User("mike", "1234");//获取信息
sser.add(users.getUsername(),users.getPassword());
//查询用户
List<User> list=sser.getInfo();
if(list.size()>0){
for (User user : list) {
System.out.println(user.getUsername()+"\t"+user.getPassword());
}
}else{
System.out.println("注册失败!");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
======================services实现类===========================
package cn.qgc.ch05.web.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import cn.qgc.ch05.web.HibernateSessionFactory;
import cn.qgc.ch05.web.WebInteface;
import cn.qgc.ch05.web.entity.User;
public class WebIntefaceImpl implements WebInteface {
//实现天气接口
public String wether() {
Random random=new Random();
int ran=random.nextInt(3);
String message=null;
switch (ran) {
case 0:
message="晴天";
break;
case 1:
message="雨天";
break;
case 2:
message="阴天";
break;
default:
message="未知";
break;
}
return message;
}
}
========jsp==========
<%@page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.codehaus.xfire.service.Service"%>
<%@page import="org.codehaus.xfire.client.XFireProxyFactory"%>
<%@page import="org.codehaus.xfire.XFireFactory"%>
<%@page import="org.codehaus.xfire.service.binding.ObjectServiceFactory"%><%@page import="cn.qgc.ch05.web.WebInteface"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<%
Service service=new ObjectServiceFactory().create(WebInteface.class);
//代理工厂
XFireProxyFactory factory=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
String Url="http://localhost:8080/web/services/CommonService";//web地址
WebInteface web=(WebInteface)factory.create(service,Url);//根据接口和地址返回服务类
%>
</head>
<body>
<h4>广州,今天:<%=web.wether()%></h4>
</body>
</html>
==============web.xml==================
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- web中央控制器配置 -->
<servlet>
<servlet-name>xFireWebServlet</servlet-name>
<servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xFireWebServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>
===========services.xml==================
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>CommonService</name>
<namespace>www.jbaptech.com.cn/CommonService</namespace>
<serviceClass>cn.qgc.ch05.web.WebInteface</serviceClass>
<implementationClass>cn.qgc.ch05.web.impl.WebIntefaceImpl</implementationClass>
</service>
</beans>