新建一个web项目webservices,以下是web service类Hello.java源代码
package com.cissco.services; import javax.jws.WebMethod; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; @WebService(targetNamespace="http://www.cissco.cn") @SOAPBinding(style=SOAPBinding.Style.RPC) public class Hello { @WebMethod public String sayHello(String name){ System.out.println(name); return name; } }
新建一个servlet,WebServiceStart.java
package com.cissco.services; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.xml.ws.Endpoint; public class WebServiceStart extends HttpServlet { /** * */ private static final long serialVersionUID = -1752174558311959922L; public WebServiceStart() { super(); // TODO Auto-generated constructor stub } @Override public void destroy() { // TODO Auto-generated method stub super.destroy(); } @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.println("准备启动服务"); Endpoint.publish("http://localhost:3030/HelloService", new Hello()); System.out.println("服务启动完毕"); } }
然后在web.xml中配置servlet
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>webservices</display-name> <servlet> <servlet-name>WebServletStart</servlet-name> <servlet-class>com.cissco.services.WebServiceStart</servlet-class> <load-on-startup>1</load-on-startup> </servlet> </web-app>
启动Tomcat服务器,如果能看到以下内容,说明webservices发布成功
2008-5-22 16:28:03 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\Java\jdk1.6.0_05\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:\Java\jdk1.6.0_05\bin\..\jre\bin\client;D:\Java\jdk1.6.0_05\bin\..\jre\bin;D:\Java\jdk1.6.0_05\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\ATI Technologies\ATI Control Panel
2008-5-22 16:28:11 org.apache.coyote.http11.Http11BaseProtocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2008-5-22 16:28:11 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 8422 ms
2008-5-22 16:28:11 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2008-5-22 16:28:11 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/5.5.25
2008-5-22 16:28:11 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
准备启动服务
服务启动完毕
2008-5-22 16:28:14 org.apache.coyote.http11.Http11BaseProtocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2008-5-22 16:28:14 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2008-5-22 16:28:14 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/94 config=null
2008-5-22 16:28:14 org.apache.catalina.storeconfig.StoreLoader load
信息: Find registry server-registry.xml at classpath resource
2008-5-22 16:28:15 org.apache.catalina.startup.Catalina start
信息: Server startup in 4047 ms