原文祭上:http://www.cnblogs.com/xdp-gacl/p/4259481.html
一.服务端java代码搭建
1.书写webService接口
package me.gacl.ws; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public interface WebServiceI { @WebMethod String sayHello(String name); @WebMethod String save(String name,String pwd); }
2.书写实现类
package me.gacl.ws; import javax.jws.WebService; @WebService public class WebServiceImpl implements WebServiceI{ @Override public String sayHello(String name) { // TODO Auto-generated method stub return "sayHellp"+name; } @Override public String save(String name, String pwd) { // TODO Auto-generated method stub return "save Sucess"; } }
3.发布服务端
package me.gacl.test; import javax.xml.ws.Endpoint; import me.gacl.ws.WebServiceImpl; public class WebServicePublish { public static void main(String[] args) { String address = "http://10.4.17.129:12345/webService_02/Webservice"; Endpoint.publish(address, new WebServiceImpl()); System.out.println("webService发布成功"); } }
运行java环境
4.在浏览器端输入地址:
http://10.4.17.129:12345/webService_02/Webservice 得到界面:
在地址后加 ?wsdl
http://10.4.17.129:12345/webService_02/Webservice?wsdl
得到下列视图
二.服务端servlet搭建
package me.gacl.web.controller; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.xml.ws.Endpoint; import me.gacl.ws.WebServiceImpl;
//将value属性设置为空字符串,这样WebServicePublishServlet就不提供对外访问的路径
//loadOnStartup属性设置WebServicePublishServlet的初始化时机
@WebServlet(value="",loadOnStartup=0) public class WebServicePublishServlet extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; public void init()throws ServletException{ String address = "http://10.4.17.129:12345/wbeService_01/WebService"; Endpoint.publish(address, new WebServiceImpl()); System.out.println("使用WebServicePublishServlet发布成功WebServicePublishServlet"); } }
通过tomcat部署后,打开方式与wsdl访问方式与上述一样,不在赘述