编写后台代码:
package test; import javax.jws.WebParam; import javax.jws.WebService; @WebService public class Web1 { public String sayHello(@WebParam(name="description")String name) { return "Hello, " + name + "!"; } }
在WEB-INF下编写sun-jaxws.xml(NetBeans自动添加)
<?xml version="1.0" encoding="UTF-8"?> <endpoints version="2.0" xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"> <endpoint implementation="test.Web1" name="Web1" url-pattern="/Web1"/> </endpoints>
在web.xml中添加如下代码(NetBeans自动添加):
<listener> <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class> </listener> <servlet> <servlet-name>Web1</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Web1</servlet-name> <url-pattern>/Web1</url-pattern> </servlet-mapping>
cmd在项目根目录下运行wsgen -cp . -keep test.Web1
拷贝test.jaxws下的SayHello.java、SayHelloResponse.java(直接拿编译好的也行)
启动tomcat,在浏览器内输入http://localhost:?端口号?/?项目名?/Web1?wsdl查看是否成功
————————————————————————————————————————————————
也可不用tomcat发布
public static void main(String[] args) { EndPoint.publish("http://localhost:?端口号?/?项目名?/Web1", new Web1()); }