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

本文介绍如何使用NetBeans5.5.1和Sun Application Server 9搭建瘦客户端调用Web服务的过程。从软件准备到具体步骤详述,并提供示例代码说明如何通过JSP页面调用Web服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

()瘦客户端(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,请参考<<JAVA EE 5平台上实现Web服务实例(模拟股票行情机)>>

6.出现如下提示窗口:




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



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

  1.  



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 --%>


  1.  

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 --%>


  1.  

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




 


()瘦客户端(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,请参考<<JAVA EE 5平台上实现Web服务实例(模拟股票行情机)>>

6.出现如下提示窗口:




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



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

  1.  



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 --%>


  1.  

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 --%>


  1.  

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




 


()瘦客户端(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,请参考<<JAVA EE 5平台上实现Web服务实例(模拟股票行情机)>>

6.出现如下提示窗口:




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



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

  1.  



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 --%>


  1.  

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 --%>


  1.  

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




 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值