将 Web 服务与 Java Servlet 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5917372/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 13:25:53  来源:igfitidea点击:

Using a Web-Service with Java Servlets

javaweb-servicesjspservletswsdl

提问by Murat Derya ?zen

I'm trying to develop a very simple Java web application using JSP and Servlets.

我正在尝试使用 JSP 和 Servlet 开发一个非常简单的 Java Web 应用程序。

1)There is a textbox and a submit button on the page,
2)The user enters his name, say John, to the textbox and clicks the button,
3)The string is forwarded to my servlet,
4)At the doPost method of my servlet, I access the posted string variable,
5)The web service I'll use has a sayHellomethod that takes an argument and returns "Hello "concatenated with the argument,
6)So, I call the sayHellomethod of the web-service, get the returned variable and forward this to a JSP, which basically writes Hello John.

1) 页面上有一个文本框和一个提交按钮,
2)用户在文本框中输入他的名字,比如约翰,然后单击按钮,
3)字符串被转发到我的 servlet,
4)在 doPost 方法中我的 servlet,我访问发布的字符串变量,
5)我将使用的 Web 服务有一个sayHello方法,该方法接受一个参数并返回"Hello "与该参数连接的返回值,
6)因此,我调用sayHello了 Web 服务的方法,获取返回的变量并将其转发到 JSP,它基本上将Hello John.

I'm familiar with the JSP and Servlet thing, but I don't know how to use an already existing web-service, or how to make use of a functionality that is already implemented in that web-service.

我熟悉 JSP 和 Servlet 的事情,但我不知道如何使用已经存在的 web 服务,或者如何利用已经在该 web 服务中实现的功能。

All I have is the name of the method, sayHello, the URL of the web service, http://example.com/hello_serviceand a link to a wsdlfile which contains xml-like code that I do not know how to make use of.

我所拥有的只是方法的名称、sayHelloWeb 服务的 URLhttp://example.com/hello_service以及指向wsdl包含我不知道如何使用的类似 xml 代码的文件的链接。

My question is, how do I make use of that web service, or how do I call a method inside a servlet?

我的问题是,如何使用该 Web 服务,或者如何调用 servlet 内的方法?

Thanks in advance.

提前致谢。

采纳答案by BalusC

I'm using Eclipse for JavaEE Developers. How do I generate a client automatically?

我正在为 JavaEE 开发人员使用 Eclipse。如何自动生成客户端?

Drop the WSDL file in your dynamic web project (or create a new project for it), rightclick it, choose Web Services > Generate Client, complete the wizard with default settings. A new package will be created where the generated WSDL client code is been placed. One of those classes have a ServiceLocatorin the classname.

将 WSDL 文件拖放到您的动态 Web 项目中(或为其创建一个新项目),右键单击它,选择Web Services > Generate Client,使用默认设置完成向导。将在放置生成的 WSDL 客户端代码的位置创建一个新包。其中一个类的类名中有一个ServiceLocator

In the servlet, you need to instantiate the ServiceLocatorclass, get the SOAP service from it and then invoke the desired methods on it. Further detail can't be given since the WSDL is unknown.

在 servlet 中,您需要实例化ServiceLocator该类,从中获取 SOAP 服务,然后在其上调用所需的方法。由于 WSDL 未知,因此无法提供更多细节。

See also:

也可以看看:

回答by sul

you have to create client stubs which will be part of your code project (which has the servlet). The WSDL defines how to generate these stubs. The you can call the methods in the stub from your servlet. You can use a variety of tools to generate these stubs, Axis2 is one of the most widely used.

您必须创建客户端存根,该存根将成为您的代码项目(其中包含 servlet)的一部分。WSDL 定义了如何生成这些存根。您可以从 servlet 调用存根中的方法。您可以使用多种工具来生成这些存根,Axis2 是使用最广泛的工具之一。

Hereis the apache Axis2 documentation which tell you how to do it.

是 apache Axis2 文档,它告诉您如何操作。

This stub will have the methods that the wsdl has defined. You will basically call these methods and internally the stub implementation (autogenerated from wsdl by axis2) will create the SOAP request based on the arguments you pass to the method. Then it will send this request over HTTP or HTTPS to the webservice URL. You will feel like you're calling code that resides on your machine, but internally it makes the call to remote webservice.

该存根将具有 wsdl 定义的方法。您将基本上调用这些方法,并且在内部存根实现(由axis2 从wsdl 自动生成)将根据您传递给方法的参数创建SOAP 请求。然后它将通过 HTTP 或 HTTPS 将此请求发送到 Web 服务 URL。你会觉得你正在调用驻留在你机器上的代码,但它在内部调用远程 web 服务。

回答by Kal

You can use "wsimport" from jax-ws to generate a client jar for the web-service. Then, including the client jar in your classpath, you can call the web service just like you would call any regular method.

您可以使用 jax-ws 中的“wsimport”为 Web 服务生成客户端 jar。然后,在您的类路径中包含客户端 jar,您可以像调用任何常规方法一样调用 Web 服务。