(三)Java EE 5实现Web服务(Web Services)及多种客户端实例-瘦客户端

来源:互联网 发布:python list 编辑:程序博客网 时间:2024/05/15 21:24

()瘦客户端(thin client)调用Web服务

瘦客户端指采用浏览器作客户端, 而代码实现通常采用Web应用程序.


准备及安装软件

1JAVA SE 5 (JDK 1.5)及以上版本(http://java.sun.com/javase/downloads/index.jsp )

2NetBeans 5.5.1 (http://zh-cn.netbeans.org/index_zh_CN.html )

3Sun Application Server 9 (https://glassfish.dev.java.net/): 可以直接下载带Sun Application Server 9NetBeans (http://www.netbeans.info/downloads/index.php )


建立瘦客户端调用Web服务


1。打开NetBeans 5.5.1, 菜单“文件”中选择“新建项目”

2。“新建项目”窗口中,“类别”栏中选择“Web”,“项目“栏中选择“Web应用程序”,点击“下一步”按钮。

3。“新建Web应用程序 ”窗口中,在“项目名称”栏中输入“ThinClient”

“服务器”中选择“Sun Application Server 9”

Java Ee版本”中选择“Java EE 5”

4。接下来在“ThinClient”项目中建立Web Serv ices Client . 鼠标右键点击“”项目,选择“新建”->“文件/文件夹”

5。“新建文件”窗口中,“类别”选择“Web服务”,“文件类型”选择“Web服务客户端”。点击“下一步”。




5。“新建Web服务客户端”窗口中,输入如下:

WSDL URL: http://localhost:8080/StockQuoteWS/StockQuoteService?WSDL

包名:com.sun.sdn.demo.ws.client 


关于WSDL URL,请参考<平台上实现Web服务实例(模拟股票行情机)>>

6.出现如下提示窗口:




7.选择"是"之后,NetBeans 自动在客户端生成Web服务的调用类.在”文件"Tab, 可以在”ThinClient”->"build"->"generated"->"wsimport",查看自动生成的调用类



8.接下来,在JSP文件中调用Web 服务.在ThinClientindex.jsp源代码窗口中,鼠标右键点击任何一位置,在菜单中选择”Web服务客户端资源”->"调用Web服务操作"

     



9. 在"选择要调用的操作"窗口中,选择"getQuote"方法.



10. index.jsp中自动生成的调用代码如下:

<%-- start web service invocation --%>


<%

try {

com.sun.sdn.demo.ws.client.StockQuoteService service = new com.sun.sdn.demo.ws.client.StockQuoteService();

com.sun.sdn.demo.ws.client.StockQuote port = service.getStockQuotePort();

// TODO initialize WS operation arguments here

java.lang.String arg0 = "";

// TODO process result here

java.lang.String result = port.getQuote(arg0);

out.println("Result = "+result);

} catch (Exception ex) {

// TODO handle custom exceptions here

}

%>

<%-- end web service invocation --%>


     

11. 为了顺利运行index.jsp, 要在jsp中加入输入框,来接收股票代码信息.修改后的index.jsp代码如下

<%@page contentType="text/html"%>

<%@page pageEncoding="UTF-8"%>



"http://www.w3.org/TR/html4/loose.dtd">

JSP Page

StockQuote Web Services Thin Client

请输入股票代码:

<%-- start web service invocation --%>


<%

String stockCode = request.getParameter("stockcode");

if (stockCode != null){

try {

com.sun.sdn.demo.ws.client.StockQuoteService service = new com.sun.sdn.demo.ws.client.StockQuoteService();

com.sun.sdn.demo.ws.client.StockQuote port = service.getStockQuotePort();

java.lang.String result = port.getQuote(stockCode);

%>

Stock <%=stockCode%> quote is <%=result%>

<%

} catch (Exception ex) {

ex.printStackTrace();

}

out.close();

}

%>

<%-- end web service invocation --%>


     

12. 鼠标右键点击"ThinClient,选择"运行项目",运行结果如下图.




 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1753368


原创粉丝点击