简体   繁体   English

将Web服务与Java Servlet一起使用

[英]Using a Web-Service with Java Servlets

I'm trying to develop a very simple Java web application using JSP and Servlets. 我正在尝试使用JSP和Servlets开发一个非常简单的Java Web应用程序。

1) There is a textbox and a submit button on the page, 1)页面上有一个文本框和一个提交按钮,
2) The user enters his name, say John, to the textbox and clicks the button, 2)用户在文本框中输入他的名字,比如约翰,然后点击按钮,
3) The string is forwarded to my servlet, 3)字符串被转发到我的servlet,
4) At the doPost method of my servlet, I access the posted string variable, 4)在我的servlet的doPost方法中,我访问发布的字符串变量,
5) The web service I'll use has a sayHello method that takes an argument and returns "Hello " concatenated with the argument, 5)我将使用的web服务有一个sayHello方法,它接受一个参数并返回与参数连接的"Hello "
6) So, I call the sayHello method of the web-service, get the returned variable and forward this to a JSP, which basically writes Hello John . 6)所以,我调用web服务的sayHello方法,获取返回的变量并将其转发给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_service and a link to a wsdl file which contains xml-like code that I do not know how to make use of. 我所拥有的只是方法的名称, sayHello ,Web服务的URL, http://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. 提前致谢。

I'm using Eclipse for JavaEE Developers. 我正在使用Eclipse for JavaEE Developers。 How do I generate a client automatically? 如何自动生成客户端?

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. 删除动态Web项目中的WSDL文件(或为其创建一个新项目),右键单击它,选择Web Services> Generate Client ,使用默认设置完成向导。 A new package will be created where the generated WSDL client code is been placed. 将创建一个新包,其中放置了生成的WSDL客户端代码。 One of those classes have a ServiceLocator in the classname. 其中一个类在类名中有一个ServiceLocator

In the servlet, you need to instantiate the ServiceLocator class, get the SOAP service from it and then invoke the desired methods on it. 在servlet中,您需要实例化ServiceLocator类,从中获取SOAP服务,然后在其上调用所需的方法。 Further detail can't be given since the WSDL is unknown. 由于WSDL未知,因此无法给出进一步的细节。

See also: 也可以看看:

You can use "wsimport" from jax-ws to generate a client jar for the web-service. 您可以使用jax-ws中的“wsimport”为Web服务生成客户端jar。 Then, including the client jar in your classpath, you can call the web service just like you would call any regular method. 然后,在类路径中包含客户端jar,您可以像调用任何常规方法一样调用Web服务。

you have to create client stubs which will be part of your code project (which has the servlet). 您必须创建客户端存根,它将成为您的代码项目(具有servlet)的一部分。 The WSDL defines how to generate these stubs. WSDL定义了如何生成这些存根。 The you can call the methods in the stub from your servlet. 您可以从servlet中调用存根中的方法。 You can use a variety of tools to generate these stubs, Axis2 is one of the most widely used. 您可以使用各种工具生成这些存根,Axis2是使用最广泛的工具之一。

Here is the apache Axis2 documentation which tell you how to do it. 是apache Axis2文档,它告诉你如何做到这一点。

This stub will have the methods that the wsdl has defined. 此存根将具有wsdl已定义的方法。 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. 您基本上将调用这些方法,并在内部执行存根实现(由axis2从wsdl自动生成)将根据您传递给方法的参数创建SOAP请求。 Then it will send this request over HTTP or HTTPS to the webservice URL. 然后,它将通过HTTP或HTTPS将此请求发送到Web服务URL。 You will feel like you're calling code that resides on your machine, but internally it makes the call to remote webservice. 您会觉得您正在调用驻留在您计算机上的代码,但在内部它会调用远程Web服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM