开发者

Using a Web-Service with Java Servlets

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

1) There is a textbox and a submit button on the page,

2) The开发者_如何学Go 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 sayHello method that takes an argument and returns "Hello " concatenated with the argument,

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.

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.

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.

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

Thanks in advance.


I'm using 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. A new package will be created where the generated WSDL client code is been placed. One of those classes have a ServiceLocator in the classname.

In the servlet, you need to instantiate the ServiceLocator class, 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.

See also:

  • Eclipse - Creating Web Service Client (Eclipse's own tutorial does it bit differently)


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.


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.

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

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜