web service实例(向银行服务支付金额)

在这个web service实例中包含3个角色,客户、中间商、服务提供商。开发环境tomcat6.0,myeclipse8.5。

客户:org.apache.axis2.eclipse.codegen.plugin_1.6.2.jar;21个jar包

中间商:axis2.war(只需将该包放入tomcat的webapps,tomcat启动时会自动解压缩)

服务提供商:插件org.apache.axis2.eclipse.service.plugin_1.6.2.jar

javax.xml.bind_2.1.9.v201005080401.jar

javax.xml.stream_1.0.1.v201004272200.jar

javax.xml_1.3.4.v201005080400.jar

将org.apache.axis2.eclipse.codegen.plugin_1.6.2.jar,org.apache.axis2.eclipse.service.plugin_1.6.2.jar,javax.xml.bind_2.1.9.v201005080401.jar,javax.xml.stream_1.0.1.v201004272200.jar,javax.xml_1.3.4.v201005080400.jar放入myeclipse的dropins目录下。

一、编写服务提供商类Pay.java

public class Pay {
	
	/**
	 * @param bankCode 银行编号
	 * @param bankCard 卡号
	 * @param money 金额
	 * @return 是否充值成功
	 */
	public boolean doPay(String bankCode,String bankCard,double money){
		
		boolean flag=false;
		
		if("CMB".equalsIgnoreCase(bankCode)){
			
			System.out.println("招商银行 ");
			flag=true;
			
		}else if("CIB".equalsIgnoreCase(bankCode)){
			
			System.out.println("兴业银行 ");
			flag=true;
			
		}else if("HZCBB2C".equalsIgnoreCase(bankCode)){
			
			System.out.println("杭州银行 ");
			flag=true;
			
		}else if("SPABANK".equalsIgnoreCase(bankCode)){
			
			System.out.println("平安银行 ");
			flag=true;
			
		}else{
			
			System.out.println("该银行未提供合作,充值失败 ");
			
		}
		return flag;
	}
}

二、创建Axis2 Service Archive

选择服务供应商项目下的classes目录




class name选择Pay的完整路径名


导出路径选择axis2的services目录



三、创建客户项目PayBao,导入jar包

index.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</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="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript">
		
		function goPay(){
			
			// 弹出页面 	路径,名称,页面参数
			window.open(	'pay.jsp', 
							'a1', 
							'height=200, width=300, top=150, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no') 
			
		}
		
	</script>
  </head>
  
  <body>
  	当前用户余额: ${sessionScope.money!=null?sessionScope.money:0 }<br/>
    <input type="button" value="充 值" οnclick="goPay()">
  </body>
</html>
支付页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</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="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript">
		
		function goPay(){
			
			// 弹出页面 	路径,名称,页面参数
			window.open(	'pay.jsp', 
							'a1', 
							'height=200, width=300, top=150, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no') 
			
		}
		
	</script>
  </head>
  
  <body>
  	当前用户余额: ${sessionScope.money!=null?sessionScope.money:0 }<br/>
    <input type="button" value="充 值" οnclick="goPay()">
  </body>
</html>

四、创建Axis2 Code Generator’

axis2服务地址

导出包地址选择客户端项目根目录



五、编写PayServlet

package control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch1.BankServiceStub;
import ch1.DoPay;
import ch1.DoPayResponse;



public class PayServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		int money=new Integer(request.getParameter("money")).intValue();
		
		String bankCode="CMB";
		String bankCard="6226010156458796";
		
		
		// 创建一个桩
		BankServiceStub bankServiceStub=new BankServiceStub();
		
		
		// 创建需要访问的方法,并且添加参数
		DoPay doPay=new DoPay();
		doPay.setBankCard(bankCard);
		doPay.setBankCode(bankCode);
		doPay.setMoney(money);
		
		// 通过桩去调用需要的方法,传递参数过去
		DoPayResponse doPayResponse=bankServiceStub.doPay(doPay);
		
		// 获得该方法的返回值
		boolean flag=doPayResponse.get_return();

		
		// 判断是否充值成功
		if(flag){
			
			if(request.getSession().getAttribute("money")!=null){
				
				int rel=((Integer)request.getSession().getAttribute("money")).intValue()+money;
				
				request.getSession().setAttribute("money", rel);
				
			}else{
				
				request.getSession().setAttribute("money", money);
				
			}
		}
		response.sendRedirect("index.jsp");
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		doGet(request, response);
		
	}
}

至此完成!








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值