第一范文网 - 专业文章范例文档资料分享平台

axis+webservice教程

来源:用户分享 时间:2025/5/18 7:03:10 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

编写Customer Exception的代码。

Java代码

package com.chnic.exception;

import java.rmi.RemoteException;

public class NoSuchUserException extends RemoteException {

private String errorMessage = \ private int id;

private static final long serialVersionUID = 1L;

public NoSuchUserException() { }

public void printErrorMessage(){

System.out.println(errorMessage + id); }

public int getId() { return id; }

public void setId(int id) { this.id = id; } }

NoSuchUserException这个类会记录在数据库没有相应数据的ID的值,然后返回给Client。值得注意的是,因为这个是个远程异常。所以要继承RemoteException这个类。两个要transfer的Bean完成之后。我们来编写Service Ojbect的代码。

Java代码

package com.chnic.webservice;

import java.sql.Connection;

import java.sql.PreparedStatement; import java.sql.ResultSet;

import java.sql.SQLException;

import com.chnic.bean.UserBean;

import com.chnic.exception.NoSuchUserException; import java.sql.DriverManager;

public class CheckUserInfo {

private String url = \ private String user = \

private String password = \

public CheckUserInfo(){ }

public Connection getConn(){ try {

Class.forName(\

return DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }

return null; }

public UserBean checkUser(int id) throws NoSuchUserException{ Connection conn = null; try {

conn = this.getConn();

PreparedStatement statement =

conn.prepareStatement(\ statement.setInt(1, id);

ResultSet rs = statement.executeQuery(); boolean flag = false; UserBean user = null;

while(rs.next()){ flag = true;

user = new UserBean(); user.setId(id);

user.setName(rs.getString(2)); }

rs.close(); if(flag)

return user; else{

NoSuchUserException userException = new NoSuchUserException();

userException.setId(id);

throw userException; }

} catch (SQLException e) { e.printStackTrace(); }finally{

this.closeConn(conn); }

return null; }

public void closeConn(Connection conn){ try {

conn.close();

} catch (SQLException e) { e.printStackTrace(); } } }

因为是Demo代码,代码写的比较粗糙,反正就是为了个演示。大家能看出来效果就好了。代码很简单,接收到一个id,然后在数据库里做匹配。如果找到匹配的了返回那个userbean,如果没找到就throw一个Exception出去。在这里多嘴一句。传递的Bean赋值的时候一定要用setXXX方法,不能用构造函数传递,否则传递过去之后属性值会丢失。 你编写的那个Bean一定要严格遵循JavaBean规范。

之后我们来看WSDD发布文件。比起之前我们看到的WSDD文件,这次的稍微有点点复杂。

Xml代码

xmlns:java=\

http://faults.samples

qname=\ xmlns:operNS=\ returnQName=\

returnType=\

xmlns:rtns=\

xmlns:tns=\

xmlns:fns=\

class=\ type=\ xmlns:tns=\

type=\

serializer=\ deserializer=\ encodingStyle=\

serializer=\ deserializer=\ encodingStyle=\

首先不同的是多了个命名空间也就是namespace节点,等会测试代码中会看到用途。除了namespace之外还有operation这个节点和里面的parameter和fault子节点。先来介绍operation这个节点的属性。

name:操作名称或者方法名称,这个值会和你server发布的相关方法名匹配,所以要和方法名相同。

qname:针对这个operation的限定名。

xmlns:针对这个qname的命名空间也就是namespace。(不明白的可以看上一篇博文)

returnQName:这个元素节点所对应的方法返回出来对象的Qname。

returnType:返回类型,注意和下面的typemapping的qname比较。

parameter节点是这个operation指代的方法的参数,fault节点代表要这个方法要抛出的异常。异常也需要被mapping。下面的typemapping做的也是这样的事情。这两个元素的节点的属性和operation都是类似,对以一下大概就知道什么意思了。在这里也不多解释了。现在来看测试代码。

Java代码

package com.chnic.test;

import java.rmi.RemoteException;

import javax.xml.namespace.QName; import javax.xml.rpc.Call; import javax.xml.rpc.Service;

import javax.xml.rpc.ServiceException; import javax.xml.rpc.ServiceFactory;

import javax.xml.rpc.encoding.TypeMapping;

import javax.xml.rpc.encoding.TypeMappingRegistry;

import org.apache.axis.encoding.ser.BeanDeserializerFactory; import org.apache.axis.encoding.ser.BeanSerializerFactory;

import com.chnic.bean.UserBean;

import com.chnic.exception.NoSuchUserException;

public class TestException {

public static void main(String[] args){ String uri = \

String serviceName = \ ServiceFactory serviceFactory;

String url = \ try {

serviceFactory = ServiceFactory.newInstance(); QName serQ = new QName(uri, serviceName);

Service service = serviceFactory.createService(serQ);

搜索更多关于: axis+webservice教程 的文档
axis+webservice教程.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c1zuqb7qnnb9acj39pw6l_4.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top