Java 如何在 .jsp 页面中创建 Web 服务客户端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2611758/
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
How to create web service client in a .jsp page?
提问by vikram
I have created a WSDL for my web service. I would like to know how to call it from a jsp page from my another web application.
我已经为我的 Web 服务创建了一个 WSDL。我想知道如何从另一个 Web 应用程序的 jsp 页面调用它。
I would like to call the web service from the jsp.. for example considering i have very simple web service which would display back the text entered in my index.jsp page after clicking submit, how would I use the wsdl url to call the web service taking the text value when clicked submit.
我想从 jsp 调用 web 服务 .. 例如,考虑到我有一个非常简单的 web 服务,它会在单击提交后显示在我的 index.jsp 页面中输入的文本,我将如何使用 wsdl url 调用 web单击提交时获取文本值的服务。
/vikram
/维克拉姆
回答by balexandre
回答by Will Hartung
Are you talking about calling it from the Browser, or calling it from the JSP to render something in the HTML sent to the browser? Those are completely different problems.
您是在谈论从浏览器调用它,还是从 JSP 调用它以呈现发送到浏览器的 HTML 中的某些内容?这些是完全不同的问题。
If you're talking about calling it from a browser, the hot tip is to treat the SOAP payload as a big blob of XML boiler plate. Then fill in the few pieces of information necessary to distinguish the SOAP request, and then use an XMLHttpRequest to send the XML to the server. Finally you then pull the result from the SOAP return payload.
如果您正在谈论从浏览器调用它,那么热点提示是将 SOAP 有效负载视为一大块 XML 样板。然后填写区分SOAP请求所需的几条信息,然后使用XMLHttpRequest将XML发送到服务器。最后,您从 SOAP 返回负载中提取结果。
If you want to just call the web service from the JSP, the best bet is to make a utility wrapper class that wraps up all of the plumbing to make the web service call, and then call that wrapper from the JSP using standard Java technique.
如果您只想从 JSP 调用 Web 服务,最好的办法是制作一个实用程序包装器类,该包装器类包装所有管道以进行 Web 服务调用,然后使用标准 Java 技术从 JSP 调用该包装器。
Edit -- answering question
编辑 - 回答问题
So, basically you have a index.jsp page with a text box. You hit submit, you want the text of that submit sent to a web service, and the result displayed back to the browser.
所以,基本上你有一个带有文本框的 index.jsp 页面。您点击提交,您希望将提交的文本发送到 Web 服务,并将结果显示回浏览器。
Simply, barring using the XHLHttpRequest, you want to create a Web Service Client (using JAX-WS, or Axis, or any of the other Java Web service tool kits).
简单地说,除非使用 XHLHttpRequest,否则您希望创建一个 Web 服务客户端(使用 JAX-WS、Axis 或任何其他 Java Web 服务工具包)。
Then you would have a Servlet or JSP take the the POST request from the form, extract the text from the request, and then it would call the web service. Finally it would render the result back to the client (using a JSP or whatever).
然后,您将有一个 Servlet 或 JSP 从表单中获取 POST 请求,从请求中提取文本,然后它会调用 Web 服务。最后,它会将结果呈现回客户端(使用 JSP 或其他方式)。
You can't POST an HTML directly to a Web Service, the protocols are different.
您不能将 HTML 直接发布到 Web 服务,协议不同。
So
所以
text text
| --> | | ----> |
| | Servlet |result | Web Service
| | | <---- |
Browser | | |
| | | forward |
| | | ------> | JSP
| |
| rendered result |
| <---------------------- |
回答by Pascal Thivent
I really do not recommend coding any kind of logic in a JSP, including calling a web service, this is not a good practice. JSP is a view technology and should be used for the presentation, not for business logic. Instead, you should submit the form to a Servlet, retrieve the submitted parameters, call the web service and then print the results in a JSP view. But let's close the parenthesis.
我真的不建议在 JSP 中编写任何类型的逻辑,包括调用 Web 服务,这不是一个好习惯。JSP 是一种视图技术,应该用于表示,而不是用于业务逻辑。相反,您应该将表单提交给 Servlet,检索提交的参数,调用 Web 服务,然后在 JSP 视图中打印结果。但是让我们关闭括号。
Since you mentioned WebLogic and Workshop in a comment, I'll assume you're using them :) WebLogic supports JAX-WS so I suggest using it for your client.
由于您在评论中提到了 WebLogic 和 Workshop,我假设您正在使用它们 :) WebLogic 支持 JAX-WS,因此我建议将它用于您的客户端。
Basically, you need to generate the "client artifacts" first (i.e. the classes that you'll use to invoke the web service). One way to do this is to use the clientgen
Ant task. Refer to Invoking a Web Service from a Stand-alone Client: Main Stepsfor details (it should be possible to generate the classes from Workshop but I can't tell you how, I don't use it).
基本上,您需要首先生成“客户端工件”(即您将用于调用 Web 服务的类)。一种方法是使用clientgen
Ant 任务。有关详细信息,请参阅 从独立客户端调用 Web 服务:主要步骤(应该可以从 Workshop 生成类,但我无法告诉您如何生成,我不使用它)。
Once the client artifacts generated, calling the web service is a piece of cake. The code will be similar to the following:
一旦生成了客户端工件,调用 Web 服务就是小菜一碟。该代码将类似于以下内容:
ComplexService test = new ComplexService(),
ComplexPortType port = test.getComplexPortTypePort();
BasicStruct in = new BasicStruct();
in.setIntValue(999);
in.setStringValue("Hello Struct");
BasicStruct result = port.echoComplexType(in);
System.out.println("echoComplexType called. Result: " + result.getIntValue() + ", " + result.getStringValue());