通过ajax与jquery访问webservice服务端数据.这里涉及到域的问题。但这不是重点!
首先我们发布一个接口:
接口类:
package com.azj.service; import javax.jws.WebService; @WebService public interface oderSel { public String getName(String name); }
接口实现代码:
package com.azj.service; import javax.jws.WebService; @WebService(endpointInterface="com.azj.service.oderSel",serviceName="orderSeiImpl") public class orderSeiImpl implements oderSel { @Override public String getName(String name) { return "你好:"+name; } }
发布:
package com.azj.service; import javax.xml.ws.Endpoint; public class Te { public static void main(String[] args) { EndpointImpl endimpl=(EndpointImpl)Endpoint.publish("http://localhost:8080/fb", new orderSeiImpl());
endimpl.getInInterceptors().add(new logininterceptor()); System.out.println("发布成功!"); } }
在游览器上输入访问http://localhost:8080/fb?wsdl 看是否生成xml文档。
访问页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>艾壹系统-AYXT</title> <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/pu.ico"/> <link type="text/css" rel="stylesheet" media="all" href="<%=request.getContextPath()%>/styles/global.css" /> <link type="text/css" rel="stylesheet" media="all" href="<%=request.getContextPath()%>/styles/global_color.css" /> <script src="/ajaxsel/js/jquery-3.0.0.min.js" type="text/javascript"></script> <script> $(function(){ $("#tj").click(function(){ //clientServlet一个servlet,这里必须通过servlet来进行访问 $.post("clientServlet",{"name":$("#name").val()},function(data){ alert($(data).text()); }); }); }); </script> </head> <body> <center> username:<input type="text" id="name"/> <input type="button" id="tj" value="jquery.click"/> </center> </body> </html>
下面是servlet类:主要的东西
package com.client.servlet; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; 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 clientServlet */ @WebServlet("/clientServlet") public class clientServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String data="<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:getName xmlns:ns2=\"http://interceptor.service.com/\"><arg0>"+name+"</arg0></ns2:getName></soap:Body></soap:Envelope>";
//webservice参数,这里的参数哪里来的呢,这里为啥qian部分服务端发布的代码里添加
//endimpl.getInInterceptors().add(new logininterceptor())的原因啦,你必须先进行访问一次,你可以通过wsdl文档生成相应的类,进行调用(较笨的一种方法)你也可以通过开发工具里的webservice浏览器进行访问,效果一样的,服务端就会生成一段响应数据的代码:在开发工具的Console下:
/*<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://wether/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <q0:send> <arg0>rainjm</arg0> </q0:send> </soapenv:Body> </soapenv:Envelope>*/
//通过修饰下就是上面所展示的。 URL url=new URL("http://localhost:8080/fb");//webservice请求路径 HttpURLConnection con=(HttpURLConnection) url.openConnection();//HttpsURLConnection进行跨域请求 //con.setRequestMethod("post");//请求方式 con.setDoOutput(true);//是否获取写入服务端数据 con.setDoInput(true);//是否接受服务端数据; con.setRequestProperty("Content-type", "text-xml;charset=utf-8");//设置写入编码方式 OutputStream out = con.getOutputStream(); out.write(data.getBytes("utf-8"));//写入数据 int code = con.getResponseCode(); if(code==200){//判断是否请求成功 InputStream in = con.getInputStream(); System.out.println(in.available()); response.setContentType("text-xml;charset=utf-8");//响应数据编码 ServletOutputStream outputStream = response.getOutputStream();//写入数据到ajax里去 byte[]buff=new byte[1024]; int len=0; while((len=in.read(buff))!=-1){ outputStream.write(buff, 0, len); } outputStream.flush(); } } }
这下就完成啦。
