转自 http://blog.csdn.net/tsun7263/archive/2009/10/16/4677490.aspx
所使用的Eclipse版本Eclipse JEE
3.3,JDK版本1.5,Tomcat6.0。在这个Eclipse版本里已集成了Axis1.4的插件,无需安装任何插件就可以开发Web
Service应用程序了,利用图形化的向导可以开发Web Service服务端和客户端程序。
    建立一个项目AxisServerDemo,写一个简单的类: 
package
demo.axis; 
public class User 
{ 
    public String
sayHello(String userName) 
    { 
        return "hello " +
userName; 
    } 
} 
    下面用Axis向导把这个bean发布为Web Service: 
    1、选择File|New|Other,选择新建Web Service。 
 
图1  选择新建Web Service
   
2、在第一步的界面上点击“next”,出现一个界面: 
 
图2  指定对哪个类生成Web Service
    点击“Web service Apache
Axis”,出现以下界面: 
 
图3  选择Axis版本
    此处选择Apache Axis。 
    对图2界面上选择要生成的类,点击“next”。 
    3、选择要生成Web
Service的方法,如下图: 
 
图4  选择要生成Web Service的方法
    4、在如下界面,启动应用服务器。 
 
图5  启动应用服务器
    在应用服务器启动后,点击“next”。 
    5、在Web
Service发布界面上,因为仅仅是一个测试,不选择发布,点击“finish”。 
 
图6  选择发布Web Service
   
按步骤完成以后,会在项目中生成一些文件,web.xml也已被修改,在lib中会多出来几个.jar文件,最后目录结构如下: 
 
图7  生成Web Service后的目录结构
   
把项目部署到tomcat中,启动后在浏览器中输入“http://localhost:8080/AxisServerDemo/services
/User?wsdl”,能够看到xml格式wsdl就说明成功了。 
    刚才完成的Web
Service发布了一个简单的功能,如何利用它呢,利用这个功能的就是Web Service 客户端。下面利用Axis1.4插件自动生成访问Web
Service的代码。 
   
新建一个项目AxisClientDemo,用以下步骤生成Web Service客户端代码: 
   
1、选择File|New|Other,选择新建Web Service Client。 
 
图8  选择新建Web Service Client
    2、在接下来的界面中,输入Web
Service所在的URL,此处是“http://localhost:8080/AxisServerDemo/services
/User?wsdl”。 
 
图9  输入Web Service的wsdl所在的路径
    3、选择生成代码所在的路径。 
 
图10  选择生成代码所在的路径
   
4、点击“Finish”,就会生成几个java文件,lib目录下面会多出几个.jar文件。生成之后的代码结构如下: 
 
图11  生成Web Service客户端代码后的目录结构
    为了测试Web Service客户端代码是否工作正常,在建立一个jsp文件: 
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="demo.axis.User"%> 
<%@page import="demo.axis.UserProxy"%>
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title> 
</head> 
<body>
<form method="POST"> 
<input type="text" name="username"/>
<br/> 
<input type="submit" value="ok"/> 
</form>
<% 
String userName = request.getParameter("username"); 
if (userName != null && !"".equals(userName))
{ 
    User user = new UserProxy(); 
    out.print(user.sayHello(userName));
} 
%> 
</body> 
</html> 
    启动tomcat,在浏览器中运行上面的jsp文件,就可以看到效果了。 
	posted on 2010-08-03 11:09 
无声 阅读(8534) 
评论(1)  编辑  收藏  所属分类: 
职场生活