Vivek Pandey's Blog 和 學習Java6(一) WebServices(3)在tomcat中發布文章中

簡單地整理開發 Webservice 的 Server 端和 Client 端程式的過程,

以及如何佈屬到 Tomcat Server上:

參考 Vivek 的範例,寫一個加法的 Webservice

  • Server Side
WebServiceStarter.java

public class WebServiceStarter extends HttpServlet
{

 
private static final long serialVersionUID = 5870534239093709659L;

 public WebServiceStarter() 
 {

   super();
 }

public void destroy() 
{

   super.destroy();
 }

public void init() throws ServletException{
System.out.println("\nStarting Calculator Service ......\n");
try
{

   Endpoint.publish("http://localhost:8088/calculator", new Calculator());
}
catch (Exception e)
{

   System.out.println("Caught Exception: " + e.toString());
}
System.out.println("\nCalculator Service is OK!\n");
}
}

在 Endpoint.publish 的部份,原本以為 Port 的設定應該與 Tomcat 的預設值 8080 一樣,但是在啟動 Tomcat 的時候發現會跟 Tomcat 搶 8080 Port,所以可以改成其它的port 

加法的 Service:Calculator.java 

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(targetNamespace = "http://localhost/sample")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public class Calculator {

@WebMethod
public int add(int a, int b) {
return a+b;
}
}

  • Deploy

程式編譯好後,在 Tomcat 的 webapps 目錄下建立一個 Service 的目錄,如: sample,其內在建立 WEB-INF 目錄,在此目錄裡建立 web.xml:

web.xml


WebServiceStarter
WebServiceStarter
1

 


在 WEB-INF 裡面在建立 classes 的目錄,把編譯好的 Calculator.class 和 WebServiceStarter.class 複製到裡面,重新啟動 Tomcat。檢視 Tomcat 的 log :

catalina.out

Jun 29, 2007 5:16:16 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/lib/jvm/java-6-sun-1.6.0.00/jre/lib/i386/client:/usr/lib/jvm/java-6-sun-1.6.0.00/jre/lib/i386:/usr/lib/jvm/java-6-sun-1.6.0.00/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib
Jun 29, 2007 5:16:16 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Jun 29, 2007 5:16:16 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 875 ms
Jun 29, 2007 5:16:16 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Jun 29, 2007 5:16:16 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.13

Starting Calculator Service ......


Calculator Service is OK!

Jun 29, 2007 5:16:18 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Jun 29, 2007 5:16:18 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Jun 29, 2007 5:16:18 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/43 config=null
Jun 29, 2007 5:16:18 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2077 ms

這樣 Server 端就 OK 了!

 

  • Java Client
先產生需要的 class: wsimport -keep http://localhost:8088/calculator?wsdl

CalculatorApp.java

class CalculatorApp {
public static void main(String args[]){
/**
* Instantiate the generated Service
*/ 
CalculatorService service = new CalculatorService();

/**
* Get the port using port getter method generated in CaculatorService
*/
Calculator calculatorProxy = service.getCalculatorPort();

/**
* Invoke the remote method
*/
int result = calculatorProxy.add(10, 20);
System.out.println("Sum of 10+20 = "+result);
}
}

編譯,執行,結果應該是:Sum of 10+20 = 30
arrow
arrow
    全站熱搜

    白努力電腦日記 發表在 痞客邦 留言(0) 人氣()