用cxf编写基于spring的webservice之下篇
来源:互联网 发布:linux安装jira7.22 编辑:程序博客网 时间:2024/06/05 08:03
上一篇,我们通过客户端的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>这样就可以了。
0 0
- 用cxf编写基于spring的webservice之下篇
- 用cxf编写基于spring的webservice之上篇
- WebService学习笔记-使用CXF编写基于Spring的Webservice
- spring中发布基于cxf的webservice
- webservice 基于spring的cxf发布
- WebService-CXF-Spring基于web的cxf
- 用CXF编写基于spring的web service
- 基于Spring+CXF技术开发WebService
- CXF 创建 WebService (基于 Spring)
- WebService之CXF与Spring的整合
- 基于Spring+CXF实现用户文件上传的WebService
- 基于spring+cxf实现用户文件传输的webservice
- 基于Spring和CXF的webservice开发环境搭建
- 在Spring中调用基于CXF框架的webService
- 基于Spring和CXF的webservice开发环境搭建
- Spring+CXF开发基于SOAP协议的WebService
- 基于spring+hibernate+cxf写的webservice服务,详细配置
- 基于CXF的WebService实例
- C算法-求s=a+aa+aaa+aaaa+aa...a的值
- 在Android中使用Junit测试注意事项
- Android+JNI 的编写步骤 从HelloWorld开始
- C# 下载远程文件
- MFC Radio Button 使用 分组 RadioButton 用法
- 用cxf编写基于spring的webservice之下篇
- Android Dalvik虚拟机JNI方法的注册过程分析
- CentOS──xxx is not in the sudoers file解决方法
- 用SignalR 2.0开发客服系统 续集[系列6:使用自己的连接ID]
- Eclipse cdt c++11 blade 设置
- android 界面防劫持提示
- 《黑马程序员》Java 异常
- 设置double类型显示位数
- winRT C++ WriteableBitmap^转换为byte^,再用opencv处理图片