用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
原创粉丝点击