本文假设您了解在 NetBeans IDE 5.0 中创建使用 Web 服务客户端的基本流程。基本流程将在使用 NetBeans IDE 5.0 开发 Web 服务客户端快速入门指南中介绍。在使用 NetBeans IDE 5.0 开发 Web 服务客户端快速入门指南中,使用一个非常简单的 Web 服务,调用操作不需要传递参数。因为它非常简单,因此,您不需要分析 Web 服务。在本教程中,在构建客户端之前,您使用 NetBeans IDE 5.0 提供的 Web 服务设施分析 Web 服务。由于 Web 服务非常复杂,因此您需要执行该操作。分析 Web 服务之后,您使用 NetBeans IDE 5.0 创建使用它的客户端。
在本教程中创建的客户端使用拼写检查程序 Web 服务。在本教程中您发现您对应用程序的贡献是提供要检查的文本、在 Web 服务上调用操作以及渲染结果。IDE 生成联系 Web 服务和发送文本所需的所有代码。拼写检查程序 Web 服务还关心其他事项,它识别拼错的单词并提供建议替换单词的列表。
注意: 本教程中使用的拼写检查程序 Web 服务由 CDYNE 公司提供。CDYNE 开发、销售和支持广泛的数据增强,数据质量和数据分析 Web 服务以及业务智能集成。拼写检查程序 Web 服务是 CDYNE 提供的 Web 服务之一。正如使用 NetBeans IDE 5.0 开发 Web 服务客户端快速入门指南中指出的一样,基于一个或多个 Web 服务的应用程序长度取决于 Web 服务的可用性和可靠性。但是,CDYNE 的常见问题解答指出它有“100% 可用性目标”,如果发生“自然灾害”、恐怖行为或其他大灾难,则 Web 服务通信传输到我们的辅助数据中心。NetBeans 感谢 CDYNE 对本教程的编写和对 NetBeans 开发的支持。
单击 Spell Check。Servlet 将输入的文本发送到拼写检查程序 Web 服务。然后拼写检查程序 Web 服务处理这个文本。它计算错误的单词数,识别错误的单词并生成建议替换单词的列表。完成它的工作之后,它将信息返回给 servlet,它识别该信息并产生一个报告:
熟悉了该示例后,下面从头开始看看如何构建它。
使用拼写检查程序 Web 服务
要使用 Web 服务,您需要创建一个 Web 服务客户端。对于创建 Web 服务客户端,NetBeans IDE 5.0 提供了一个客户端创建工具,即 Web Service Client 向导,该向导生成用于查询 Web 服务的代码。它还提供了用于开发创建的 Web 服务客户端的工具,即由 Projects 窗口中的节点组成的工作区域以及用于测试和分析 Web 服务的内置客户端。这些工具都是标准 NetBeans IDE 安装的一部分,它们完全可以即装即用,不需要任何插件。
创建客户端
选择 File > New Project (Ctrl-Shift-N)。在 Categories 下,选择 Web。在 Projects 下选择 Web Application。单击 Next。将该项目命名为 SpellCheckService 并确保将 Sun Java System Application Server 指定为您的目标服务器。单击 Finish。
在 Projects 窗口中,右键单击 SpellCheckService 项目节点,并选择 New > File/Folder。在 New File 向导中,选择 Web Service > Web Service Client。在 Web Service Client 向导中,指定 Web 服务的 URL:
在 Projects 窗口的 Web Service References 节点中,您应看到如下内容:
Projects 窗口显示名为“Check”的 Web 服务使操作“checkTextBody”和“suggestWord”可用于您的应用程序。“checkTextBody”操作检查字符串是否有拼写错误,并返回将由客户端处理的数据。整个教程中您都将使用该操作。将不使用用于向 Web 服务的字典建议单词的“suggestWord”操作。
有另一种方法名为 getVer,但是在本教程中您不需要使用它。另一方面,如果您将使客户端实现一般可用,并且在业务设置中真正地使用它,则运行检查条件非常有用,它检查 Web 服务的版本是否是您希望的版本,并且在版本不同时产生某些种类的错误消息,原因是随后的 Web 服务版本可能与您的客户端实现发生冲突。
检查 Words 对象
在 Test Web Service Operation 对话框中,展开 DocumentSummary 节点。现在展开 Words[] 节点及其一个子节点。现在 Test Web Service Operation 对话框的下面部分显示以下内容:
这是 Words 对象。它包含名为 Words 的数组(针对每个拼错的单词)。在上图中,您可以看到拼错单词“spell”的 Words 数组。
在 Files 窗口 (Ctrl-2) 中,展开 build 文件夹,直到您到达此处:
现在双击 Words.java 节点在 Source Editor 中打开它(同时查看上面的节点)。执行此操作向您显示什么方法可用于 Words.java。在 Source Editor 中展开它(再在 Files 窗口中查看它的扩展节点)之后,您可以看到每个拼错的单词应该指定给 Words 数组,您可以使用 Words 对象访问该数组。执行该操作之后,您可以使用 Words 对象的方法分别与每个拼错的单词进行交互。在下一部分中,您使用 Words 对象如下(下面的粗体代码高亮显示 Words 对象提供的方法):
for (int i = 0; i < allwrongwords.length; i++) {
String onewrongword = allwrongwords[i].getWord();
int onewordsuggestioncount = allwrongwords[i].getSuggestionCount();
String[] allsuggestions = allwrongwords[i].getSuggestions();
out.println("<hr><p><b>Wrong word:</b><font color='red'> " + onewrongword + "</font>");
out.println("<p><b>" + onewordsuggestioncount + " suggestions:</b><br>");
for (int k = 0; k < allsuggestions.length; k++) {
String onesuggestion = allsuggestions[k];
out.println(onesuggestion);
}
}
在浏览器中显示,上面几行代码的结果如下:
为了使事情更完美,您可以在结尾处提供一个总结:
out.println("<font color='red'><b>Summary:</b> " + no_of_mistakes + " mistakes (");
for (int i = 0; i < allwrongwords.length; i++) {
String onewrongword = allwrongwords[i].getWord();
out.println(onewrongword);
}
out.println(").");
out.println("</font>");
在浏览器中显示,上面几行代码的结果如下:
当然,一些变化是可能的。您访问所需的方法之后,可以很轻松地循环返回的数据并对其执行您希望的操作。
开发客户端
有很多方法可以实现 Web 服务客户端。Web 服务的 WSDL 文件限制您向 Web 服务发送的信息类型,并且它还限制返回时您希望接收到的信息类型。但是,WSDL 文件没有对您如何 传递它需要的信息进行限制,也没有对用户界面的组成 进行限制。下面您构建的客户端实现由允许用户输入要检查的文本的 JSP 页面和将该文本传递给 Web 服务,然后产生包含结果的报告的 servlet 组成。
右键单击 Explorer 窗口中的项目节点,选择 New > File/Folder,然后选择 Web > Servlet。单击 Next。将 servlet 命名为 SpellCheckServlet 并在 Package 下拉列表中键入 org.netbeans.end2end.check.client。单击 Next。注意,该 servlet 的 URL 映射为 /SpellCheckServlet,然后单击 Finish。Servlet 在 Source Editor 中打开。
将光标放在 Source Editor 的 processRequest 方法内,右键单击并选择 Web Service Client Resources > Call Web Service Operation。单击“Select Operation to Invoke”对话框中的 checkTextBody 操作,然后单击 OK。
现在代码位已经添加到 servlet。滚动到底部,您看到将客户端连接到 Web 服务的代码(大多数代码有红色下划线,忽略这一情况,这是一个错误,对构建和运行应用程序没有影响)。
在 processRequest 方法的顶部可看到调用 Web 服务的代码片段。该方法就是您在 Web 服务上调用操作需要使用的所有方法。
用下面的代码替换整个 processRequest 方法。下面代码的行内注释解释每行的作用。
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//Get content from JSP:
String TextArea1 = request.getParameter("TextArea1");
//Invoke the operation on the web service that does the spell check.
//As arguments, send the content and the key:
DocumentSummary doc = getCheckSoap().checkTextBody(TextArea1,"0");
//From the retrieved document summary,
//identify all the content, correct and incorrect:
String allcontent = doc.getBody();
//From the retrieved document summary,
//identify the number of wrongly spelled words:
int no_of_mistakes = doc.getMisspelledWordCount();
//From the retrieved document summary,
//identify the array of wrongly spelled words:
Words[] allwrongwords = doc.getMisspelledWord();
out.println("<html>");
out.println("<head>");
//Display the report's name as a title in the browser's titlebar:
out.println("<title>Spell Checker Report</title>");
out.println("</head>");
out.println("<body>");
//Display the report's name as a header within the body of the report:
out.println("<h2><font color='red'>Spell Checker Report</font></h2>");
//Display all the content (correct as well as incorrectly spelled) between quotation marks:
out.println("<hr><b>Your text:</b> \"" + allcontent + "\"" + "<p>");
//For every array of wrong words (one array per wrong word),
//identify the wrong word, the number of suggestions, and
//the array of suggestions. Then display the wrong word and the number of suggestions and
//then, for the array of suggestions belonging to the current wrong word, display each
//suggestion:
for (int i = 0; i < allwrongwords.length; i++) {
String onewrongword = allwrongwords[i].getWord();
int onewordsuggestioncount = allwrongwords[i].getSuggestionCount();
String[] allsuggestions = allwrongwords[i].getSuggestions();
out.println("<hr><p><b>Wrong word:</b><font color='red'> " + onewrongword + "</font>");
out.println("<p><b>" + onewordsuggestioncount + " suggestions:</b><br>");
for (int k = 0; k < allsuggestions.length; k++) {
String onesuggestion = allsuggestions[k];
out.println(onesuggestion);
}
}
//Display a line after each array of wrong words:
out.println("<hr>");
//Summarize by providing the number of errors and display them:
out.println("<font color='red'><b>Summary:</b> " + no_of_mistakes + " mistakes (");
for (int i = 0; i < allwrongwords.length; i++) {
String onewrongword = allwrongwords[i].getWord();
out.println(onewrongword);
}
out.println(").");
out.println("</font>");
out.println("</body>");
out.println("</html>");
out.close();
}