How to create web service client in a .jsp page?

27,477

Solution 1

I really do not recommend coding any kind of logic in a JSP, including calling a web service, this is not a good practice. JSP is a view technology and should be used for the presentation, not for business logic. Instead, you should submit the form to a Servlet, retrieve the submitted parameters, call the web service and then print the results in a JSP view. But let's close the parenthesis.

Since you mentioned WebLogic and Workshop in a comment, I'll assume you're using them :) WebLogic supports JAX-WS so I suggest using it for your client.

Basically, you need to generate the "client artifacts" first (i.e. the classes that you'll use to invoke the web service). One way to do this is to use the clientgen Ant task. Refer to Invoking a Web Service from a Stand-alone Client: Main Steps for details (it should be possible to generate the classes from Workshop but I can't tell you how, I don't use it).

Once the client artifacts generated, calling the web service is a piece of cake. The code will be similar to the following:

ComplexService test = new ComplexService(), 
ComplexPortType port = test.getComplexPortTypePort();

BasicStruct in = new BasicStruct();

in.setIntValue(999);
in.setStringValue("Hello Struct");

BasicStruct result = port.echoComplexType(in);
System.out.println("echoComplexType called. Result: " + result.getIntValue() + ", " + result.getStringValue());

Solution 2

Are you talking about calling it from the Browser, or calling it from the JSP to render something in the HTML sent to the browser? Those are completely different problems.

If you're talking about calling it from a browser, the hot tip is to treat the SOAP payload as a big blob of XML boiler plate. Then fill in the few pieces of information necessary to distinguish the SOAP request, and then use an XMLHttpRequest to send the XML to the server. Finally you then pull the result from the SOAP return payload.

If you want to just call the web service from the JSP, the best bet is to make a utility wrapper class that wraps up all of the plumbing to make the web service call, and then call that wrapper from the JSP using standard Java technique.

Edit -- answering question

So, basically you have a index.jsp page with a text box. You hit submit, you want the text of that submit sent to a web service, and the result displayed back to the browser.

Simply, barring using the XHLHttpRequest, you want to create a Web Service Client (using JAX-WS, or Axis, or any of the other Java Web service tool kits).

Then you would have a Servlet or JSP take the the POST request from the form, extract the text from the request, and then it would call the web service. Finally it would render the result back to the client (using a JSP or whatever).

You can't POST an HTML directly to a Web Service, the protocols are different.

So

          text           text
        | --> |         | ----> |            
        |     | Servlet |result | Web Service
        |     |         | <---- |
Browser |     |         |
        |     |         | forward |
        |     |         | ------> | JSP  
        |                         |
        |    rendered result      |
        | <---------------------- |
Share:
27,477
vikram
Author by

vikram

Updated on July 19, 2022

Comments

  • vikram
    vikram almost 2 years

    I have created a WSDL for my web service. I would like to know how to call it from a jsp page from my another web application.

    I would like to call the web service from the jsp.. for example considering i have very simple web service which would display back the text entered in my index.jsp page after clicking submit, how would I use the wsdl url to call the web service taking the text value when clicked submit.

    /vikram