6.3. Integrating an Interactive Website
and a Web Service
A Web service client
is usually not interactive and, in particular, not a browser. This section
illustrates, as proof of concept, how a browser-based client might be used with
a SOAP-based web service. The @WebService is simple and familiar:
package ch06.tc;
import javax.jws.WebService;
import javax.jws.WebMethod;
@WebService
public class TempConvert {
@WebMethod
public float c2f(float t) { return 32.0F + (t * 9.0F / 5.0F); }
@WebMethod
public float f2c(float t) { return (5.0F / 9.0F) * (t - 32.0F); }
}
After the compiled TempConvert is
copied to the ch06/tc/WEB-INF/classes/ch06/tc
directory, the WAR file is created and copied as usual to the
domains/domain1/autodeploy directory under AS_HOME.
The next step is to write the interactive
web application, which in this case consists of an HTML document, two small JSP
scripts, and a vanilla web.xml document. Here
is the HTML form for a user to enter a temperature to be converted:
<html><body>
<form method = 'post' action = 'temp_convert.jsp'>
Temperature to convert: <input type = 'text' name = 'temperature'><br/><hr/>
<input type = 'submit' value = ' Click to submit '/>
</form>
</body></html>
And here is the JSP script that
invokes the TempConvert web service to do
the temperature conversion:
<%@ page errorPage = 'error.jsp' %>
<%@ page import = 'client.TempConvert' %>
<%@ page import = 'client.TempConvertService' %>
<html><body>
<%! private float f2c, c2f, temp; %>
<%
String temp_str = request.getParameter("temperature");
if (temp_str != null) temp = Float.parseFloat(temp_str.trim());
TempConvertService service = new TempConvertService();
TempConvert port = service.getTempConvertPort();
f2c = port.f2C(temp);
c2f = port.c2F(temp);
%>
<p><%= this.temp %>F = <%= this.f2c %>C</p>
<p><%= this.temp %>C = <%= this.c2f %>F</p>
<a href = 'index.html'>Try another</a>
</body></html>
The two imported classes, client.TempConvert and
client.TempConvertService, are wsimport-generated artifacts in the
WEB-INF/classes/client subdirectory of the
directory that holds the HTML document and the two JSP scripts. For
completeness, here is the error.jsp script:
<%@ page isErrorPage = "true" %>
<html>
<% response.setStatus(400); %>
<body>
<h2><%= exception.toString() %></h2>
<p>Bad data: please try again.</p>
<p><a href = "index.html">Return to home page</a></p>
</body></html>
The web.xml deployment document is also short:
<?xml version = '1.0' encoding = 'ISO-8859-1'?>
<web-app xmlns = 'http://java.sun.com/xml/ns/javaee'
xmlns:xsi = 'http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation = 'http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd'
version = '2.5'>
<description>JSP frontend to TempConvert service</description>
<error-page>
<exception-type>java.lang.NumberFormatException</exception-type>
<location>/error.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
The HTML page and the JSP scripts
together with the compiled classes and web.xml are
then put into a WAR file:
% jar cvf tcJSP.war index.html *.jsp WEB-INF
for deployment in the GlassFish
autodeploy subdirectory. For testing, a browser can be opened to the URL
http://localhost:8081/tcJSP.
No comments:
Post a Comment