Using a web service with Java servlets

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

1) The page has a text box and a submit button,
2) The user enters his name, say John, in the text box and presses the button,
3) The string is sent to my servlet,
4) In the doPost method of my servlet, I refer to the published string variable,
5) The web service that I will use has a sayHello method that takes an argument and returns the "Hello " associated with the argument,
6) So, I call the sayHello method of the web service, get the returned variable and forward it to the JSP, which basically writes Hello John .

I am familiar with JSP and Servlet, but I do not know how to use an existing web service or how to use the functionality that is already implemented in this web service.

All I have is a method name, sayHello , a web service URL, http://example.com/hello_service and a link to a wsdl file that contains xml code that I donโ€™t know how to make use of.

My question is: how can I use this web service or how can I call a method inside the servlet?

Thanks in advance.

+6
source share
3 answers

I use Eclipse for JavaEE developers. How can I create a client automatically?

Drop the WSDL file in your dynamic web project (or create a new project for it), right-click it, select Web Services> Create Client, fill the wizard with the default settings. A new package will be created in which the generated WSDL client code will be placed. One of these classes has a ServiceLocator in the class name.

In the servlet, you need to create an instance of the ServiceLocator class, get the SOAP service from it, and then call the methods it needs. Additional information cannot be specified because the WSDL is unknown.

See also:

+5
source

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

0
source

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

Here is the apache Axis2 documentation that explains how to do this.

This stub will have the methods defined by wsdl. You will mainly call these methods, and internally the implementation of the stub (auto-generation from wsdl on axis 2) will create a SOAP request based on the arguments that you pass to the method. He will then send this request via HTTP or HTTPS to the web service URL. You will feel that you are calling a code that is on your computer, but internally it is calling a remote web service.

0
source

Source: https://habr.com/ru/post/887486/


All Articles