一、WebServices简介
1、基本介绍
WebServices是应用程序组件
WebServices使用开放协议进行通信
WebServices是独立并可以自我描述
WebServices可通过使用UDDI来发现
WebServices可被其它应用程序使用
XML是WebServices的基础
二、 工作方法: XML+HTTP
三、 WebServices平台元素
①SOAP(简单对象访问协议)
②UDDI(它是一种目录服务)
③ WSDL(Web services描述语言)
四、WebServices的优点:
可在不同的应用程序与平台之间交换数据
五、使用
WebServices开发手段
<1>使用jdk开发(1.6及以上版本)
①服务端实现
②定义一个interfac,使用@WebService注解标注接口,@WebMethod注解标注方法
③ 定义此接口的实现类,并使用@WebService注解标注
④ 使用Endpoint(终端)类发布webservice
String address = "http://localhost:8080/myws";
Endpoint.publish(address, new MyServiceImpl());
⑥ 测试服务
(1)直接在浏览器中输入: http://localhost:8080/myws?wsdl
(2) 使用Eclipse自带的工具进行测试
Launch the Web Services Explorer-->WSDL Page
注1:WSDL全名为:网络服务描述语言,它是Web Service的描述语言,它包含一系列描述某个web service的定义
<2>客户端实现
① 使用jdk的wsimport.exe(java_home\bin)工具生成客户端代码
shift+右键
wsimport -keep url?wsdl
wsimport -keep *.xml
② 调用客户端代码完成
// 创建工厂对象
WebServiceImplService factory = new WebServiceImplService();
// 通过工厂对象创建WebServiceImpl对象
WebServiceImpl webServiceImpl = factory.getWebServiceImplPort();
wsimport -keep http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
wsimport -keep http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl
案例:获取天气
1、百度搜索:webxml
截取所需要的内容
2、eclipse准备
3、action
/**
* 查询天气
* @return
* @throws ServiceException
* @throws RemoteException
*/
public String weather() throws ServiceException, RemoteException {
String city = request.getParameter("cityname");
if(city==null && "".equals(city)) {
city = "";
}
WeatherWSLocator weatherWebServiceLocator=new WeatherWSLocator();
WeatherWSSoap weatherWebServiceSoap=weatherWebServiceLocator.getWeatherWSSoap();
String[] supportCityString = weatherWebServiceSoap.getWeather(city, null);
//查询对应省份的城市
// String[] supportCityString2 = weatherWebServiceSoap.getSupportCityString(city);
request.getSession().setAttribute("city", supportCityString[1]);
request.getSession().setAttribute("date", supportCityString[7]+supportCityString[8]);
request.getSession().setAttribute("thing", supportCityString[4]);
return "service";
}
5、jsp[运用
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>jb-aptech毕业设计项目</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="<%=path %>/css/style.css" rel="stylesheet" type="text/css">
<script src="<%=path %>/script/common.js"></script>
</head>
<body style="background-image:url(<%=path %>/wj/1.jpg)">
<div class="page_title">客户服务管理</div>
<br/>
<form action="${pageContext.request.contextPath}/sy-wj/cstServiceAction_weather.action" method="post">
天气:<input name = "cityname"/>
<input type = "submit" value = "查询">
</form>
${city }
${date }
<br/>
${thing }
<br/>
<h1 align="center">欢迎来到客户服务管理中心</h1>
<div align="center">
<h3>请选择您的操作:</h3>
<a href = "<%=path %>/wj/cust/service/add.jsp">服务创建</a>
<a href = "<%=path %>/wj/cust/service/dispatch.jsp">服务分配</a>
<a href = "<%=path %>/wj/cust/service/deal.jsp">服务处理</a>
<a href = "<%=path %>/wj/cust/service/feedback.jsp">服务反馈</a>
<a href = "<%=path %>/wj/cust/service/arch.jsp">服务归档</a>
</div>
</body>
</html>
效果