Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 792
Murat Derya Özen Avatar asked Oct 12 '22 03:10

Murat Derya Özen


1 Answers

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)
like image 133
BalusC Avatar answered Nov 01 '22 19:11

BalusC