Using cxf to write the next part of spring based Web Service

Copyright notice: This is the original article of the blogger. It can't be reproduced without the permission of the blogger. https://blog.csdn.n...
Copyright notice: This is the original article of the blogger. It can't be reproduced without the permission of the blogger. https://blog.csdn.net/u010741376/article/details/48518843

In the previous article, we accessed webservice through the Java code of the client. In this article, we requested webservice through HttpURLConnection. If we used ajax to directly request webservice, there would be cross domain problems,

For example: if the address I published is localhost, then if I use IP access in the js of the page, there will be problems and I can't access it.

servlet code for page request:

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); //Request body 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);//Output from httpUrlConnection or not openConnection.setDoInput(true);// Set whether to read from httpUrlConnection openConnection.setRequestMethod("POST");//Request type openConnection.setRequestProperty("Content-Type","text/xml;charset=utf-8"); OutputStream outputStream = openConnection.getOutputStream(); outputStream.write(data.getBytes("utf-8"));//Write data to the server /** * When the corresponding code is 200, the request is successful */ int responseCode = openConnection.getResponseCode(); if(responseCode==200){ InputStream inputStream = openConnection.getInputStream();//Read data from server System.out.println("return:"+inputStream.available()); /** * Write the requested data to the page */ 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:

<%@ 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="call webservice"> </body> </html>
That's it.

3 December 2019, 12:38 | Views: 9433

Add new comment

For adding a comment, please log in
or create account

0 comments