首先启动一个WebService服务,代码如下:
SEI接口:
import javax.jws.WebMethod;
import javax.jws.WebService;
/*
* SEI接口
* */
@WebService
public interface HelloWS {
@WebMethod
public String sayHello(String name);
}
SEI实现
import javax.jws.WebService;
/*
* SEI实现
* */
@WebService
public class HelloWSImpl implements HelloWS {
@Override
public String sayHello(String name) {
System.out.println("server sayHello()"+name);
return "Hello"+name;
}
}
WebService实现:
package com.lin.ws.server;
import java.util.List;
import javax.xml.ws.Endpoint;
import org.apache.cxf.interceptor.Interceptor;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws22.EndpointImpl;
import org.apache.cxf.message.Message;
import com.lin.ws.HelloWSImpl;
/*
* 实现Web Service
* */
public class ServerTest4 {
public static void main(String[] args) {
String address="http://172.19.183.17:8888/WS/hellows";
Endpoint endpoint= Endpoint.publish(address, new HelloWSImpl());
System.out.println(endpoint);
EndpointImpl endpointImpl=(EndpointImpl) endpoint;
//服务器端的日志入拦截器
List<Interceptor<? extends Message>> inInterceptors= endpointImpl.getInInterceptors();
inInterceptors.add(new LoggingInInterceptor());
//服务器端的日志出拦截器
List<Interceptor<? extends Message>> outInterceptors= endpointImpl.getOutInterceptors();
outInterceptors.add(new LoggingOutInterceptor());
System.out.println("发布Web Service成功!");
}
}
启动后,创建一个html文件,准备通过ajax请求WebService服务,直接上代码,代码中有说明:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
//服务地址
var url="http://172.19.183.17:8888/WS/hellows";
//JQuery请求
$(function(){
$("#btn").click(function(){
var name=document.getElementById("name").value;
//请求体
var data='<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://ws.lin.com/"><arg0>'+name+'</arg0></ns2:sayHello></soap:Body></soap:Envelope>';
$.post(url, data, function(msg){
var $Result = $(msg);
var value = $Result.find("return").text();
alert(value);
},"xml");
});
});
//Ajax请求
function reqWebService(){
var name=document.getElementById("name").value;
//请求体
var data='<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:sayHello xmlns:ns2="http://ws.lin.com/"><arg0>'+name+'</arg0></ns2:sayHello></soap:Body></soap:Envelope>';
var request=getRequest();
alert(request.status);
request.onreadystatechange =function(){
if(request.readyState==4&&request.status==200){
var result=request.responseXML;
alert(result);
var returnElement = result.getElementsByTagName("return")[0];
var value = returnElement.firstChild.data;
//alert(value);
}
};
//打开连接
request.open("POST", url);
//重新设置请求头
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//发送请求
request.send(data);
}
/* ActiveXObject有浏览器兼容问题 */
function getRequest() {
var xmlhttp=null;
try{
xmlhttp=new XMLHttpRequest();
}catch (e) {
try {
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlhttp;
}
</script>
</head>
<body>
用户名:<input id="name" name="username" value=""><br>
<button onclick="reqWebService()">Ajax请求WebService</button>
<button id="btn">Jquery请求WebService</button>
</body>
</html>