Java 从jsp调用Webservice
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22811484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
calling Webservices from jsp
提问by user3489236
I have created the wsdl successfully .Its url is "http://:/aebis/HelpdeskWebserviceImpl?wsdl". Now I want to use this url to call the function in jsp.I am using Jboss as server. Please suggest if any one can help. Thanks in advance.
我已经成功创建了 wsdl。它的 url 是“http://:/aebis/HelpdeskWebserviceImpl?wsdl”。现在我想使用这个 url 来调用 jsp 中的函数。我使用 Jboss 作为服务器。请建议是否有人可以提供帮助。提前致谢。
采纳答案by Leo
Here is a 5-minute example using eclipse
这是一个使用 eclipse 的 5 分钟示例
I am gonna use this WSDL to demonstrate
我将使用这个 WSDL 来演示
http://www.webservicex.net/ConvertAcceleration.asmx?WSDL
http://www.webservicex.net/ConvertAcceleration.asmx?WSDL
Create a dynamic java project for your JSPs
为您的 JSP 创建一个动态 Java 项目
Create your JSP and some backend java class
创建您的 JSP 和一些后端 java 类
your JSP
你的 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>
and
和
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());
}
}
Now create the WS client. Click on/select the project
现在创建 WS 客户端。单击/选择项目
right click and create a new Web Service Client from the given WSDL
右键单击并从给定的 WSDL 创建一个新的 Web 服务客户端
Change MyClass to call the web service (you can test first using the class main too)
更改 MyClass 以调用 Web 服务(您也可以先使用类 main 进行测试)
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());
}
}
Add the web app to your server (if there's one. If there isn't, create a new server)
将 Web 应用程序添加到您的服务器(如果有。如果没有,请创建一个新服务器)
Clear the server (forcing it to refresh the app) and start it
清除服务器(强制它刷新应用程序)并启动它
And here it is.
就在这里。
回答by Alexander Rühl
The wsimport toolis part of the JDK and generates "portable artifacts" from a wsdl. That gives you classes to use easily for communicating with the web service without the need of doing the boilerplate code yourself.
该wsimport工具是JDK的一部分和从WSDL产生“移植工件”。这使您可以轻松使用类与 Web 服务进行通信,而无需自己编写样板代码。
But I feel that you may need some more background beforehand, to get a better understanding how to use JAX-RS web services or implement a state-of-the-art web application with JSF, so my advice is to consult the Java EE 7 tutorialfor those two (chapter 28 and 7).
但我觉得你可能需要更多的背景知识,以便更好地了解如何使用 JAX-RS web 服务或使用 JSF 实现最先进的 web 应用程序,所以我的建议是咨询Java EE 7这两个教程(第 28 章和第 7 章)。