How to use OC4J JSP Tags for Web Services
Date: 02-Jun-2003
After reading this how-to document you should be able
to:
Introduction
This document demonstrates how to call a Web Service
directly from a JavaServer Page using the tag library provided by the
OC4J's(Oracle9iAS Container's for J2EE) JSP Engine for Web Services.
Software Requirements
Description
The Web Services tag library provides functionality
to bind to a Web Service, send SOAP request and receive SOAP response
by invoking a particular operation on Web Service, send input parameters
& receive output parameters, map SOAP/XML data types to Java types
and set custom properties for use by the client application. The tag library
only supports RPC style Web Services.
The tag library supports invoking operations defined
in WSDL documents that use the W3C XML schema version with the following
namespace :
http://www.w3.org/2001/XMLSchema
Various tags defined by Web Services tag library are as follows :
webservice : This tag is used to create a Web Service proxy. The attribute
wsdlURL is set to URL of WSDL document of the Web Service. This tag,
then uses one of the following combinations :
- a binding and SOAP location (useful for a WSDL document identified
in a UDDI registry)
- a service name and port (either provided through tag attributes,
or the first service and its first port from the WSDL document)
-
map : This tag, if specified, is used
by the Web service proxy to add an entry to the SOAP mapping registry,
which is a registry that maps local SOAP/XML types to Java types.
Any numbers of map tags can be nested within a webservice tag, one
tag for each desired type mapping.
property : You can use this tag to define any of several supported
custom properties for use by the Web service client application. Each
property tag must be nested within the webservice tag, and the property
will have the same scope as the parent Web service.
invoke : Use this tag to invoke an operation of the Web service. An
invoke tag accesses a Web service proxy either by being nested within
a webservice tag, or through a scripting variable.
part : Use this tag if the operation uses input message parts, one
part tag for each input part.
In this how-to, the above tags are used to call a Web
Service named Temperature Service from xmethods.net to get the temperature
of a city by providing its zip code. The JSP code is given in following
listing :
.
<%@ page contentType="text/html"%> <%@ taglib uri="http://xmlns.oracle.com/j2ee/jsp/tld/ws/wstaglib.tld" prefix="ws" %>
<HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; "> </HEAD>
<BODY> <% String zip = "94041"; %>
<ws:webservice id="weatherservice" wsdlUrl="http://www.xmethods.net/sd/2001/TemperatureService.wsdl"
binding="TemperatureBinding" soapLocation="http://services.xmethods.net:80/soap/servlet/rpcrouter" scope="page">
<ws:property name="http.proxyHost" value="www-proxy.us.oracle.com"/> <ws:property name="http.proxyPort" value="80"/> </ws:webservice>
<ws:invoke id="temperature" webservice="weatherservice" operation="getTemp"> <ws:part name="zipcode" value="<%=zip%>"/> </ws:invoke>
<B>The current temperature for city with zipcode = <%=zip%> is :</B>
<P><b><%= temperature%> </b></P>
</BODY>
</HTML>
|
|
WebServicesTags.jsp
The JSP file can be seen here.
The <webservice> tag
provides the URL of WSDL document, the SOAP binding and SOAP location
of the Web Service. The attribute id is used to uniquely
identify the Web Service and is used by the <invoke>
tag to call a particular operation on this Web Service.
The <property> tag is used to define the proxy and
the port on which it listens.
The <invoke> tag's
operation attribute defines the method to be called on the Web Service
identified by the webservice attribute. The attribute id
defines a variable in which output (the current temperature of the city)
from the Web Service method is stored. The input (zipcode of the city
) is supplied to the method getTemp() using the <part>
tag.
The OC4J Web Services tag library uses
SOAP based RPC style mechanism and Oracle's dynamic invocation API's to
invoke and execute operations on the Web Service using the information
provided in the WSDL document of the Web Service. When the client application
acquires WSDL document at runtime, the dynamic invocation API's are used
to dynamically generate the Web Service Proxy stub class. This proxy class
exposes the methods defined in the WSDL document which are used by the
<invoke> tag to invoke the required
operation.
Preparing and Executing the Sample JSP
-
Copy the JSP file to <OC4J>/j2ee/home/default-web-app
directory.
where <OC4J> refers to directory
where OC4J is installed.
-
The implementation of Web Services tag library (wstaglib.tld)
is provided in ojsputil.jar available
in <OC4J>/j2ee/home/jsp/lib/taglib
directory.The wstaglib.tld file should
be deployed along with the application. Extract the wstaglib.tld
from ojsputil.jar and put it in the <OC4J>/j2ee/home/default-web-app/WEB-INF
directory. Add the following entry to web.xml
present in the WEB-INF directory :
<web-app>
.... <taglib> <taglib-uri>http://xmlns.oracle.com/j2ee/jsp/tld/ws/wstaglib.tld</taglib-uri> <taglib-location>/WEB-INF/wstaglib.tld</taglib-location> </taglib>
.... </web-app>
|
|
Sample web.xml
The values of <taglib-uri> should
match with the taglib URI provided in the JSP file.
-
The implementation of dynamic invocation API's is provided in dsv2.jar
file located at <OC4J>/lib directory.
The dsv2.jar and the ojsputil.jar
file should be present in the classpath.
-
Start the OC4J as follows from command prompt :
java -jar oc4j.jar
- Access the JSP page from browser as follows :
http://yourhost.com:8888/WebServicesTags.jsp
Resources
Summary
This how-to document explained how to access a Web
Service directly from a JSP client using OC4J JSP tag library for Web
Services.
|