`

Java开发WebService实例--计数器(下)

阅读更多
四、客户端编写


   客户端是个页面,为了条理清晰,我先写个调用Web Service的类,其内容如下:

package com.wallimn.WebService;

//调试请注意包名

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

public class CounterServiceClient {

	private String counterarr[];

	public boolean getCounter(String CounterName, String password) {

		boolean res = false;

		try {

			String endpoint = "http://localhost:8080/axis/WsCounterByWallimn.jws";// 此处注意,请与你的开发环境匹配

			Service service = new Service();

			Call call = (Call) service.createCall();

			call.setTargetEndpointAddress(new java.net.URL(endpoint));

			call.setOperationName("Counter");

			// 填写你要调用的方法名称

			String counter = (String) call.invoke(new Object[] { CounterName,
					password });

			counterarr = counter.split(";");

			res = (counterarr != null && counterarr.length == 4);

		}

		catch (Exception e) {

		}

		return res;

	}

	public String getDc() {

		return counterarr[3];

	}

	public String getMc() {

		return counterarr[1];

	}

	public String getTc() {

		return counterarr[0];

	}

	public String getWc() {

		return counterarr[2];

	}

}




  到页面(test.jsp)上就简单了,我也把它贴在下面:
<%@ page language="java" import="com.wallimn.WebService.CounterServiceClient" pageEncoding="GB18030"%>
<%
CounterServiceClient client = new CounterServiceClient();
client.getCounter("hello","123");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>计数器测试页面</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="wallimn,计数器,WebService">
<meta http-equiv="description" content="计数器使用示例">
</head>

<body>
<h2 align="center">计数器详情</h2>
<hr/>
总访问量:<%=client.getTc()%> <br/>
今天访问量:<%=client.getDc()%> <br/>
本周访问量:<%=client.getWc()%> <br/>
本月访问量:<%=client.getMc()%> <br/>
<hr/>
<p>欢迎交流<br/>博客:http://blog.csdn.net/wallimn<br/>电邮:wallimn@sohu.com</p>
</body>
</html>


五、结束语

  至此一个完整计数器的Web Service开发、使用的程序的全部完成了。将上面的类、及页面部署到任意的一个上下文中,通过浏览器打开test.jsp,就可以看了结果了。

  欢迎访问我的博客(http://wallimn.iteye.com、http://blog.csdn.net/wallimn)留言或发邮件(wallimn@sohu.com)交流。

/***********本人原创,欢迎转载,转载请保留本人信息*************/
作者:wallimn 电邮:wallimn@sohu.com 时间:2007-04-17
博客:http://wallimn.iteye.com
   http://blog.csdn.net/wallimn
网络硬盘:http://wallimn.ys168.com
/***********文章发表请与本人联系,作者保留所有权利*************/
 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics