2014-04-02 52 views
2

我已經成功創建了wsdl。它的url是「http://:/ aebis/HelpdeskWebserviceImpl?wsdl」。 現在我想用這個url來調用jsp.I中的函數,我使用Jboss作爲服務器。 請建議是否有人可以提供幫助。 在此先感謝。從jsp調用Webservices

+1

是否有原因,你需要在JSP內完成?如果您有機會使用最新的技術,請不要再使用JSP。您需要的是您的Web服務的客戶端,例如由JDK的wsimport工具生成的,然後可以通過JSF頁面使用。 –

+0

如何在網頁中使用wsimport工具。 – user3489236

+0

https://www.eclipse.org/webtools/jst/components/ws/1.5/tutorials/WebServiceClient/WebServiceClient.html – Leo

回答

7

下面是使用eclipse

我要去使用此WSDL來證明

http://www.webservicex.net/ConvertAcceleration.asmx?WSDL

你的JSP創建一個動態的Java項目有5分鐘的例子

enter image description here

enter image description here

enter image description here

創建你的JSP和一些後端Java類

enter image description here

你的JSP

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
</head> 
<body> 
<%= new myweb.MyClass().getResult() %> 
</body> 
</html> 

package myweb; 

public class MyClass { 

    public String getResult(){ 
     return null; 
    } 

    public static void main(String[] args) { 

     MyClass c = new MyClass(); 
     System.out.println(c.getResult()); 

    } 

} 

現在創建WS CLI ENT。點擊/選擇項目

enter image description here

單擊右鍵,從給定的WSDL

enter image description here

enter image description here

更改MyClass的調用Web服務創建一個新的Web服務客戶端(您也可以先使用班級主測試)

package myweb; 

import java.rmi.RemoteException; 

import NET.webserviceX.www.AccelerationUnitSoap; 
import NET.webserviceX.www.AccelerationUnitSoapProxy; 
import NET.webserviceX.www.Accelerations; 

public class MyClass { 

public String getResult() throws RemoteException { 
     AccelerationUnitSoap a = new AccelerationUnitSoapProxy(); 
     Accelerations x = Accelerations.decimeterPersquaresecond; 
     Accelerations y = Accelerations.centimeterPersquaresecond; 
     Object z = a.changeAccelerationUnit(1, x, y); 
     return z.toString(); 
} 

public static void main(String[] args) throws RemoteException { 

     MyClass c = new MyClass(); 
     System.out.println(c.getResult()); 

} 

} 

將Web應用程序添加到您的服務器(如果有的話)。如果沒有,創建一個新的服務器)

enter image description here

清除服務器(迫使它刷新應用程序),並啓動它

,就是這樣。

enter image description here

+0

偉大的嚮導幫助我很多。我想知道爲什麼它有這麼幾個upvotes。 –

0

wsimport tool是JDK的一部分,並從wsdl生成「便攜式工件」。這使您可以輕鬆地使用類與Web服務進行通信,而無需自己完成樣板代碼。

但是我覺得您可能需要更多的背景,以便更好地瞭解如何使用JAX-RS Web服務或使用JSF實現最先進的Web應用程序,所以我的建議是諮詢這兩部分的Java EE 7 tutorial(第28和7章)。

+0

感謝您與演示的不錯答案 – user3489236