1、概述
本例试图通过web service完成人员档案查询功能。
涉及到相关技术:JAVA XFIRE DOM4J
2、相关代码
2.1 服务器端
1
package
com.howelltech.xfire;
2
3
public
interface
IFileService
{
4
public
String getFile(String studentName);
5
}
6
1
package
com.howelltech.xfire;
2
3
import
java.util.HashMap;
4
import
java.util.Map;
5
6
public
class
FileService
implements
IFileService
{
7
public
FileService()
{}
8
9
public
String getFile(String studentName)
{
10
Map map
=
new
HashMap();
11
map.put(
"
name
"
,
"
jack
"
);
12
map.put(
"
sex
"
,
"
male
"
);
13
map.put(
"
age
"
,
"
18
"
);
14
if
(studentName.equalsIgnoreCase(
"
jack
"
))
{
15
return
(
new
Map2Xml().createDocument(map)).asXML();
16
}
else
{
17
return
""
;
18
}
19
}
20
}
21
1
package
com.howelltech.xfire;
2
3
import
java.io.IOException;
4
import
java.io.UnsupportedEncodingException;
5
import
java.util.HashMap;
6
import
java.util.Iterator;
7
import
java.util.Map;
8
import
java.util.Set;
9
10
import
org.dom4j.Document;
11
import
org.dom4j.DocumentException;
12
import
org.dom4j.DocumentHelper;
13
import
org.dom4j.Element;
14
import
org.dom4j.io.OutputFormat;
15
import
org.dom4j.io.XMLWriter;
16
17
public
class
Map2Xml
{
18
public
Document createDocument(Map map)
{
19
Document document
=
DocumentHelper.createDocument();
20
Element root
=
document.addElement(
"
root
"
);
21
22
Set set
=
map.keySet();
23
for
(Iterator iter
=
set.iterator(); iter.hasNext();)
{
24
String key
=
iter.next().toString();
25
Element el
=
root.addElement(key).addText(map.get(key).toString());
26
}
27
return
document;
28
}
29
30
public
Map parseXml(String xml)
{
31
Map map
=
new
HashMap();
32
try
{
33
Document document
=
DocumentHelper.parseText(xml);
34
Element root
=
document.getRootElement();
35
for
( Iterator i
=
root.elementIterator(); i.hasNext(); )
{
36
Element el
=
(Element) i.next();
37
map.put(el.getName(), el.getText());
38
}
39
}
catch
(DocumentException e)
{
40
e.printStackTrace();
41
}
42
return
map;
43
}
44
}
45
2.2 客户端
1
package
com.howelltech.xfire;
2
3
import
java.io.IOException;
4
import
java.net.MalformedURLException;
5
import
java.util.HashMap;
6
import
java.util.Iterator;
7
import
java.util.Map;
8
import
java.util.Set;
9
10
import
javax.servlet.ServletException;
11
import
javax.servlet.http.HttpServlet;
12
import
javax.servlet.http.HttpServletRequest;
13
import
javax.servlet.http.HttpServletResponse;
14
15
import
org.codehaus.xfire.XFire;
16
import
org.codehaus.xfire.XFireFactory;
17
import
org.codehaus.xfire.client.XFireProxyFactory;
18
import
org.codehaus.xfire.service.Service;
19
import
org.codehaus.xfire.service.binding.ObjectServiceFactory;
20
21
public
class
Client4FileService
extends
HttpServlet
{
22
23
protected
void
doGet(HttpServletRequest req, HttpServletResponse resp)
24
throws
ServletException, IOException
{
25
String name
=
req.getParameter(
"
name
"
);
26
String defName
=
"
jack
"
;
27
28
if
(
null
!=
name)
{
29
defName
=
name;
30
}
31
String responseGot
=
""
;
32
Map map
=
new
HashMap();
33
try
{
34
responseGot
=
callWebService(defName);
35
map
=
new
Map2Xml().parseXml(responseGot);
36
}
catch
(Exception e)
{
37
e.printStackTrace();
38
}
39
resp.setContentType(
"
text/html
"
);
40
java.io.PrintWriter out
=
resp.getWriter();
41
out.println(
"
<html>
"
);
42
out.println(
"
<head><title>学生档案信息</title></head><body>
"
);
43
out.println(
"
<h2>学生档案信息</h2>
"
);
44
out.println(
"
<B>Service Request</B>
"
+
"
<br>
"
+
"
Transfer funds -
"
45
+
"
<TABLE>
"
);
46
Set set
=
map.keySet();
47
for
(Iterator iter
=
set.iterator(); iter.hasNext();)
{
48
String key
=
(String) iter.next();
49
out.println(
"
<TR><TD>
"
+
key
+
"
: </TD><TD>
"
+
map.get(key)
50
+
"
</TD></TR>
"
);
51
}
52
out.println(
"
</TABLE>
"
);
53
out.println(
"
<br>
"
+
"
<B>Response Received</B>
"
+
"
<br>
"
+
responseGot);
54
out.println(
"
</body></html>
"
);
55
}
56
57
/**/
/*
58
* call the web service
59
*
60
*/
61
public
String callWebService(String name)
throws
Exception
{
62
63
//
create a metadata of the service
64
Service serviceModel
=
new
ObjectServiceFactory()
65
.create(IFileService.
class
);
66
67
//
create a proxy for the deployed service
68
XFire xfire
=
XFireFactory.newInstance().getXFire();
69
XFireProxyFactory factory
=
new
XFireProxyFactory(xfire);
70
//
String serviceUrl = "xfire.local:
//
file" ;
71
String serviceUrl
=
"
http://localhost:8080/xfire/services/file
"
;
72
IFileService client
=
null
;
73
try
{
74
client
=
(IFileService) factory.create(serviceModel, serviceUrl);
75
}
catch
(MalformedURLException e)
{
76
e.printStackTrace();
77
}
78
79
//
invoke the service
80
String serviceResponse
=
""
;
81
try
{
82
serviceResponse
=
client.getFile(name);
83
}
catch
(Exception e)
{
84
e.printStackTrace();
85
}
86
87
return
serviceResponse;
88
}
89
90
/**/
/*
91
* doPost():
92
*/
93
protected
void
doPost(HttpServletRequest req, HttpServletResponse resp)
94
throws
ServletException, IOException
{
95
doGet(req, resp);
96
}
97
}
98
2.3 配置文件
1
<?
xml version="1.0" encoding="ISO-8859-1"
?>
2
3
<!
DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"
>
4
5
<
web-app
>
6
<
servlet
>
7
<
servlet-name
>
XFireServlet
</
servlet-name
>
8
<
display-name
>
XFire Servlet
</
display-name
>
9
<
servlet-class
>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</
servlet-class
>
10
</
servlet
>
11
<
servlet
>
12
<
servlet-name
>
WsClient
</
servlet-name
>
13
<
display-name
>
WsClientServlet
</
display-name
>
14
<
description
></
description
>
15
<
servlet-class
>
com.howelltech.xfire.Client4FileService
</
servlet-class
>
16
</
servlet
>
17
18
<
servlet-mapping
>
19
<
servlet-name
>
XFireServlet
</
servlet-name
>
20
<
url-pattern
>
/servlet/XFireServlet/*
</
url-pattern
>
21
</
servlet-mapping
>
22
<
servlet-mapping
>
23
<
servlet-name
>
XFireServlet
</
servlet-name
>
24
<
url-pattern
>
/services/*
</
url-pattern
>
25
</
servlet-mapping
>
26
<
servlet-mapping
>
27
<
servlet-name
>
WsClient
</
servlet-name
>
28
<
url-pattern
>
/ws
</
url-pattern
>
29
</
servlet-mapping
>
30
31
</
web-app
>
services.xml
1
<
beans
xmlns
="http://xfire.codehaus.org/config/1.0"
>
2
<
service
>
3
<
name
>
file
</
name
>
4
<
namespace
>
studentFile
</
namespace
>
5
<
serviceClass
>
com.howelltech.xfire.IFileService
</
serviceClass
>
6
<
implementationClass
>
com.howelltech.xfire.FileService
</
implementationClass
>
7
</
service
>
8
</
beans
>
9
3、部署相关
3.1 类库
java类库,xfire类库,dom4j类库
3.2 目录结构
WEB-INF
--- web.xml
--- lib
--- classes
--- com.howelltech.xfire.*.class
--- META-INF
--- xfire
services.xml
3.3 测试url
http://localhost:8080/xfire/ws
4、总结
感觉xfire实现web service还是挺容易的,不过实际项目中会遇到哪些问题,还有待以后的验证。下次继续思考客户端与服务器端交互问题,比如修改服务器端信息,传输文件、图片等数据。