In this section, we will create a JSP Web Service client for the HelloWorld
web service. To create a JSP, select File | New | Other. In New, select Web | JSP File and click on Next, as shown in the following screenshot:
In the New JSP File wizard, select the webapp
folder, specify File name as JAXWSClient.jsp
, and click on Next, as shown in the following screenshot:
Select the New JSP file (html) template and click on Finish. The JAXWSClient.jsp
file gets added to the webapp
folder. In the client JSP, we will invoke the web service with a name and display the web service response in the browser. First, create a URL object for the WSDL. The URL for a web service is constructed from the context root + endpoint:
URL wsdlLocation = new URL("http://localhost:8080/jboss-jaxws/HelloWorld?WSDL");
The jboss-jaxws
in the URL is the context root and the /HelloWorld
is the servlet mapping URL for the web service endpoint as specified in the web.xml
. Next, create a QName
object for the service name. A QName
represents a qualified name. Specify arguments to the QName
constructor as the target namespace http://org.jboss.jaxws.service/
and the service name HelloWorldService
:
QName serviceName = new QName("http://org.jboss.jaxws.service/","HelloWorldService");
The client view of a web service is provided by a javax.xml.ws.Service
object. Create javax.xml.ws.Service
from the WSDL location URL and QName
for the service name:
Service service = Service.create(wsdlLocation, serviceName);
Get a proxy to the web service using the getPort(Class endpointInterface)
method. Specify the endpoint interface class as org.jboss.jaxws.service.HelloWS.class
:
HelloWS port = service.getPort(org.jboss.jaxws.service.HelloWS.class);
Invoke the sayHello
method of the web service proxy with a name as an argument. The sayHello
method returns a String
. Output the response from the web service:
String result = port.sayHello("John Smith"); out.println(result);
The JAXWSClient.jsp
is listed here:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@ page import="org.jboss.jaxws.service.*,javax.xml.ws.WebServiceRef,java.net.URL,javax.xml.namespace.QName,javax.xml.ws.Service"%> <html> <head> <meta http-equiv="Content-Type" content="text/xml; charset=windows-1252" /> <title>JAXWS Client</title> </head> <body> <%URL wsdlLocation = new URL ( "http://localhost:8080/jboss-jaxws/HelloWorld?WSDL"); QName serviceName = new QName("http://org.jboss.jaxws.service/", "HelloWorldService"); Service service = Service.create(wsdlLocation, serviceName); HelloWS port = service.getPort(org.jboss.jaxws.service.HelloWS.class); String result = port.sayHello("John Smith"); out.println(result); %> </body> </html>
The directory structure of the jboss-jaxws
application is shown in Java EE web project in the following screenshot: