用cxf編寫基於spring的webservice之下篇
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/u010741376/article/details/48518843
上一篇,我們通過客戶端的Java程式碼訪問webservice,這一篇,我們通過HttpURLConnection請求webservice,如果使用ajax直接請求webservice,會存在跨域的問題,
比如:如果我釋出的地址是localhost的,那麼在頁面的js裡使用IP訪問的話,就會有問題,訪問不了。
頁面請求的servlet程式碼:
package com.cxf.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HttpURLConnectionServlet
*/
@WebServlet("/HttpURLConnectionServlet")
public class HttpURLConnectionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HttpURLConnectionServlet() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String code=request.getParameter("code");
System.out.println("code:"+code);
//請求體
String data="<soap:Envelope xmlns:soap=`http://schemas.xmlsoap.org/soap/envelope/`><soap:Body><ns2:getOrderById xmlns:ns2=`http://ws.cxf.com/`><arg0>"+code+"</arg0></ns2:getOrderById></soap:Body></soap:Envelope>";
URL url=new URL("http://localhost:8088/cxf_spring_ws/ws/orderws");
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
openConnection.setDoOutput(true);//是否從httpUrlConnection輸出
openConnection.setDoInput(true);// 設定是否從httpUrlConnection讀入
openConnection.setRequestMethod("POST");//請求型別
openConnection.setRequestProperty("Content-Type","text/xml;charset=utf-8");
OutputStream outputStream = openConnection.getOutputStream();
outputStream.write(data.getBytes("utf-8"));//向服務端寫入資料
/**
* 當相應碼為200的時候,說明請求成功
*/
int responseCode = openConnection.getResponseCode();
if(responseCode==200){
InputStream inputStream = openConnection.getInputStream();//讀取服務端的資料
System.out.println("return:"+inputStream.available());
/**
* 將請求得到的資料寫到頁面
*/
byte[] buffer=new byte[1024];
response.setContentType("text/xml;charset=utf-8");
ServletOutputStream os = response.getOutputStream();
int len;
while((len=inputStream.read(buffer))>0){
os.write(buffer, 0, len);
}
os.flush();
}
}
}
jsp頁面:
<%@ page language="java" import="java.util.*" 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>
<base href="<%=basePath%>">
<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">
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function(){
$("#bt").click(function(){
var code=$("#code").val();
$.post(
`HttpURLConnectionServlet`,
{"code":code},
function(msg){
var $msg=$(msg);
var info=$msg.find("return").text();
alert(info);
},
`xml`
);
});
});
</script>
</head>
<body>
<input type="text" id="code">
<input type="button" id="bt" value="呼叫webservice">
</body>
</html>
這樣就可以了。
相關文章
- WebService之Spring+CXF整合示例WebSpring
- 基於Maven的Spring整合CXFMavenSpring
- 使用CXF與Spring整合實現RESTFul WebServiceSpringRESTWeb
- maven+CXF+Spring+tomcat 開發webserviceMavenSpringTomcatWeb
- 使用瀏覽器位址列呼叫CXF Webservice的寫法瀏覽器Web
- Spring整合CXF,釋出RSETful 風格WebServiceSpringWeb
- Spring7:基於註解的Spring MVC(下篇)SpringMVC
- CXF實現webService服務(一)Web
- 開發:隨筆記錄之 CXF 動態呼叫Webservice筆記Web
- cxf WebService設定wsdl中soapAction的值Web
- Unix系列shell程式編寫:下篇
- cxf設定代理訪問webservice介面Web
- Cxf - Spring整合Cxf暴露WebServicesSpringWeb
- 基於 Spring Boot 2.0 構建一個 RESTful WebServiceSpring BootRESTWeb
- apache cxf-2.4.3 +spring-3.0.5釋出SOAP協議WebService服務ApacheSpring協議Web
- CXF建立webservice客戶端和服務端Web客戶端服務端
- CXF入門教程(1) -- 第一個webServiceWeb
- cxf ServerFactoryBean 生成基於soap1.2的WebServicesServerBeanWeb
- CXF入門教程(5) -- webService非同步呼叫模式Web非同步模式
- 編寫基於jQuery的外掛的方法jQuery
- WebService框架大比較(Axis,axis2,Xfire以及cxf)Web框架
- CXF--整合SpringSpring
- Java-Spring-WebService最基礎的配置示例JavaSpringWeb
- 用匯編輕鬆地寫基於對話方塊的程式 (轉)
- linux下用C編寫的基於smtp的郵件傳送程式Linux
- 基於python編寫的天氣抓取程式Python
- 基於nodejs編寫小爬蟲NodeJS爬蟲
- Cxf - Spring整合呼叫WebServicesSpringWeb
- 基於順豐同城介面編寫sdk,java三方sdk編寫思路Java
- Android 如何編寫基於編譯時註解的專案Android編譯
- 基於spring boot框架進行二次封裝,微型框架編寫思路Spring Boot框架封裝
- 編寫基於PHP擴充套件庫的後門PHP套件
- Cxf - 基礎和示例
- 基於MFC的編寫執行緒快速入門 (轉)執行緒
- 實踐基於REST風格的Webservice(PHP,C#)RESTWebPHPC#
- WebService基於SoapHeader實現安全認證WebHeader
- 尋找寫程式碼感覺(三)之使用 Spring Boot 編寫介面Spring Boot
- Spring3.2 + cxf1.7.3整合Spring