用cxf编写基于spring的webservice之下篇

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.
版权声明:本文为博主原创文章,未经博主允许不得转载。 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>
这样就可以了。

相关文章
|
9月前
phpstorm插件应用:Test RESTful WEB Service 控制台接口调试工具
phpstorm插件应用:Test RESTful WEB Service 控制台接口调试工具
117 0
|
1月前
|
存储 缓存 算法
关于 Service Worker 和 Web 应用对应关系的讨论
关于 Service Worker 和 Web 应用对应关系的讨论
12 0
|
2月前
|
Java API Apache
Apache CXF生成WebService的客户端
Apache CXF生成WebService的客户端
|
6月前
|
JSON 安全 API
使用 ABAP sproxy 事务码生成的 Proxy 消费 Web Service
使用 ABAP sproxy 事务码生成的 Proxy 消费 Web Service
54 0
|
2月前
|
XML 网络架构 数据格式
Ruby 教程 之 Ruby Web Service 应用 - SOAP4R 2
Ruby Web Service 应用 - SOAP4R
24 5
|
2月前
|
XML Linux 网络架构
Ruby 教程 之 Ruby Web Service 应用 - SOAP4R 1
Ruby Web Service 应用 - SOAP4R
23 3
|
8月前
|
XML Java API
Java Web Service Get请求使用指南
Java Web Service Get请求使用指南 在当今互联网时代,Web Service已经成为了现代软件开发中不可或缺的一部分。而Java作为一种广泛使用的编程语言,自然也提供了丰富的工具和库来支持Web Service的开发。本文将为大家介绍如何使用Java编程语言进行Web Service的Get请求。
86 0
|
4月前
|
Java 数据库连接 Apache
SpringBoot整合CXF实现WebService
SpringBoot整合CXF实现WebService
123 0
|
4月前
|
XML Java 应用服务中间件
WebService - Axis2与Spring整合并发布多个service(同样使用services.xml)
WebService - Axis2与Spring整合并发布多个service(同样使用services.xml)
75 0
|
4月前
|
Java 应用服务中间件 Spring
WebService - Axis2使用services.xml进行开发server与client(未与Spring整合)
WebService - Axis2使用services.xml进行开发server与client(未与Spring整合)
42 0