概述
开发环境:jdk7+eclipse+axis2+tomcat7+MySQL5
操作系统:win7
一、服务端
eclipse集成axis2插件,将图片中的这两个压缩包(网上搜资源)解压到eclipse根目录下的plugins文件里面,重启eclipse;
安装tomcat,将axis2.war文件copy到tomcat根目录下的webapps文件中,在eclipse中配置tomcat,启动tomcat,如出现如下界面则安装成功;
编写要发布的服务代码:
数据库–建库建表语句:
CREATE DATABASE student;
DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
LOCK TABLES `admin` WRITE;
INSERT INTO `admin` VALUES (1,'admin','admin','admin');
UNLOCK TABLES;
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`sno` varchar(20) NOT NULL,
`department` varchar(20) NOT NULL,
`hometown` varchar(20) NOT NULL,
`mark` varchar(20) NOT NULL,
`email` varchar(20) NOT NULL,
`tel` varchar(20) NOT NULL,
`sex` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;
LOCK TABLES `student` WRITE;
INSERT INTO `student` VALUES (18,'张三','001','信息科学技术学院','辽宁','80','zhangsan@163.com','13888888888','男'),(19,'李四','002','理学院','上海','70','lisi@sina.com','13812341234','男'),(20,'王五','003','外国语学院','北京','88','wangwu@126.com','13698765432','女');
UNLOCK TABLES;
服务StudentService
package doc_03_axis.server;
/**
* 模块说明: web service interface
*
*/
public class StudentService {
public String say(String str) {
return str;
}
public String sayHello() {
return "hello world";
}
// query
public Student queryBySno(String sno) {
StudentDAO sd = StudentDAO.getInstance();
return sd.queryBySno(sno);
}
// add
public boolean add(Student stu) {
StudentDAO sd = StudentDAO.getInstance();
return sd.add(stu);
}
// delete
public boolean deleteBySno(String sno) {
StudentDAO sd = StudentDAO.getInstance();
return sd.deleteBySno(sno);
}
// update
public boolean update(Student stu) {
StudentDAO sd = StudentDAO.getInstance();
return sd.update(stu);
}
}
数据访问StudentDAO
package doc_03_axis.server;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 模块说明:对外接口
*
*/
public class StudentDAO {
private final DBUtil db = DBUtil.getDBUtil();
private ResultSet rs;
private static StudentDAO sd = null;
private StudentDAO() {
}
public static synchronized StudentDAO getInstance() {
if (sd == null) {
sd = new StudentDAO();
}
return sd;
}
// add
public boolean add(Student stu) {
boolean result = false;
if (stu == null) {
return result;
}
try {
// insert
String sql = "insert into student(name,sno,age,math,chinese,english) values(?,?,?,?,?,?)";
Object[] param = { stu.getName(), stu.getSno(), stu.getAge(), stu.getMath(), stu.getChinese(),
stu.getEnglish() };
if (db.executeUpdate(sql, param) == 1) {
result = true;
}
} finally {
destroy();
}
return result;
}
// delete
public boolean deleteBySno(String sno) {
boolean result = false;
if (sno == null || "".equals(sno)) {
return result;
}
String sql = "delete from student where sno=?";
String[] param = { sno };
int rowCount = db.executeUpdate(sql, param);
if (rowCount == 1) {
result = true;
}
destroy();
return result;
}
// update
public boolean update(Student stu) {
boolean result = false;
if (stu == null) {
return result;
}
try {
// update
String sql = "update student set name=?,age=?,math=?,chinese=?,english=? where sno=?";
Object[] param = { stu.getName(), stu.getAge(), stu.getMath(), stu.getChinese(), stu.getEnglish(),
stu.getSno() };
int rowCount = db.executeUpdate(sql, param);
if (rowCount == 1) {
result = true;
}
} finally {
destroy();
}
return result;
}
// query by sno
public Student queryBySno(String sno) {
Student result = null;
if ("".equals(sno) || sno == null) {
return result;
}
String sql = "select * from student where sno=?";
String[] checkParam = { sno };
rs = db.executeQuery(sql, checkParam);
try {
if (rs.next()) {
result = new Student();
result.setAge(rs.getInt("age"));
result.setChinese(rs.getInt("chinese"));
result.setEnglish(rs.getInt("english"));
result.setMath(rs.getInt("math"));
result.setName(rs.getString("name"));
result.setSno(rs.getString("sno"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
destroy();
}
return result;
}
private void destroy() {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException se) {
se.printStackTrace();
} finally {
db.close();
}
}
}
实体类Student
package doc_03_axis.server;
/**
* 模块说明: 实体类Student
*
*/
public class Student {
private String name; // 名字
private int age; // 年龄
private String sno; // 学号
private int math; // 数学成绩
private int chinese; // 语文成绩
private int english; // 英语成绩
public int getTotal() {
return (math + chinese + english);
}
public int getAvg() {
return getTotal() / 3;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public int getMath() {
return math;
}
public void setMath(int math) {
this.math = math;
}
public int getChinese() {
return chinese;
}
public void setChinese(int chinese) {
this.chinese = chinese;
}
public int getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
}
数据库工具类DBUtil
package doc_03_axis.server;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 模块说明:数据库工具类
*
*/
public class DBUtil {
private static DBUtil db;
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
private DBUtil() {
}
public static DBUtil getDBUtil() {
if (db == null) {
db = new DBUtil();
}
return db;
}
public int executeUpdate(String sql) {
int result = -1;
if (getConn() == null) {
return result;
}
try {
ps = conn.prepareStatement(sql);
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public int executeUpdate(String sql, Object[] obj) {
int result = -1;
if (getConn() == null) {
return result;
}
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
ps.setObject(i + 1, obj[i]);
}
result = ps.executeUpdate();
close();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public ResultSet executeQuery(String sql) {
if (getConn() == null) {
return null;
}
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
public ResultSet executeQuery(String sql, Object[] obj) {
if (getConn() == null) {
return null;
}
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
ps.setObject(i + 1, obj[i]);
}
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
private Connection getConn() {
try {
if (conn == null || conn.isClosed()) {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncodeing=UTF-8", "root",
"root");
}
} catch (ClassNotFoundException e) {
System.out.println("jdbc driver is not found.");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void close() {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
4、 在tomcat上部署服务,步骤如下:
eclipse–file–new–other–axis2 service archiver
StudentService为对外接口
部署到D:Javaapache-tomcat-7.0.65webappsaxis2WEB-INFservices
根据具体路径,自行设置!!!
Finish.
5、非常重要的一步!!!
jdbc连接mysql所需的驱动包,必须copy到D:Javaapache-tomcat-7.0.65webappsaxis2WEB-INFlib下,否则客户端调用会报错!!!
6、浏览器输入http://http://localhost:8080/axis2/services/listServices,出现如下界面则部署成功:
二、客户端
1.导入axis2提供的jar包
2.客户端代码
package doc_03_axis.client;
import javax.xml.namespace.QName;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import doc_03_axis.server.Student;
/**
* 模块说明: 使用RPC生成客户端调用已经发布的Web Service
*
*/
public class RPCClient {
private static final String URL = "http://localhost:8080/axis2/services/MyService";
private static final String TARGET_NAMESPACE = "http://server.doc_03_axis";
private static final String METHOD = "add";
public static void main(String[] args) {
RPCClient client = new RPCClient();
// 给出方法的参数值
Student stu = new Student();
stu.setAge(10);
stu.setChinese(80);
stu.setEnglish(90);
stu.setMath(70);
stu.setName("wangwu");
stu.setSno("003");
Object[] param = { stu };
// 给出方法返回值的数据类型的Class对象
Class[] classes = { boolean.class };
// call
Object result = client.call(URL, TARGET_NAMESPACE, METHOD, param, classes);
System.out.println("is successful?: " + result);
}
public Object call(String url, String targetNamespace, String method, Object[] param, Class[] classes) {
Object obj = null;
try {
// 使用RPC调用Web服务
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 给出要调用的Web服务URL
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
// 给出要调用的方法及WSDL文件的命名空间
QName opAddEntry = new QName(targetNamespace, method);
obj = serviceClient.invokeBlocking(opAddEntry, param, classes)[0];
} catch (AxisFault e) {
e.printStackTrace();
}
return obj;
}
}
运行结果:
is successful?: true
仅调用服务的add方法,其他方法可以自行实验,仅需更改调用所需的参数即可。
end.
最后
以上就是灵巧过客为你收集整理的使用axis技术创建webservice(服务端+客户端)的全部内容,希望文章能够帮你解决使用axis技术创建webservice(服务端+客户端)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复