Java Reference
In-Depth Information
Consuming a Web Service from a JSP
Problem
You have a JSP that you want to act as a client of a web service.
Solution
Generate a client based on the WSDL and include those resources within your WAR deploy-
ment. Use a scriptlet in the JSP to instantiate the client.
Discussion
In your JSP application, make sure the web service is deployed by testing the WSDL URL in
a browser. Use the WSDL location to generate a client with wsimport either during your build
or at the command line. Write a scriptlet that invokes the client, just as you would within a
servlet. Include the client classes with your WAR when you deploy.
NOTE
As with any other Java class-based resource that your web application uses, it does not matter if you
package your classes together as a library and include them in WEB-INF/libor whether you just place
the classes directly in the WEB-INF/classesdirectory. Of course, regular classloader concerns apply
as usual.
Example 6-7 is the JSP code that executes after following these steps.
Example6-7.Invoking a web service from a JSP
<%@page import="com.soacookbook.client.CatalogServiceSN,
com.soacookbook.client.CatalogService"
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>JSP Client</title></head>
<body>
<h2>Catalog Service Client</h2>
<%
try {
Search WWH ::




Custom Search