目录:
- 基于JDK6 jax-ws开发的webservice获取客户端IP地址
- Endpoint.publish() 轻量级HTTP服务发布
- 在web容器tomcat下发布
- 基于XFire开发的webservice获取客户端IP地址
- 基于Axis开发的webservice获取客户端IP地址
[一]、基于JDK6 jax-ws开发的webservice获取客户端IP地址
以:http://www.micmiu.com/soa/webservice/jax-ws-demo/ 中的[三] 2 具体示例为基础:
1. 情况一:如果以 Endpoint.publish() 的方式发布:
服务端接口实现类:HelloServiceImpl.java 修改如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
packagecom.micmiu.jaxws.demo.impl;
importjava.net.InetSocketAddress;
importjavax.annotation.Resource;
import javax.jws.WebMethod;
importjavax.jws.WebParam;
import javax.jws.WebService;
importjavax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import com.micmiu.jaxws.demo.HelloService;
importcom.sun.net.httpserver.HttpExchange;
import com.sun.xml.internal.ws.developer.JAXWSProperties;
/**
* blog http://www.micmiu.com
*
* @author Michael
*/
@WebService()
public classHelloServiceImpl implements HelloService{
@Resource
privateWebServiceContext wsContext;
@WebMethod
publicString sayHello(@WebParam(name= "userName")String userName){
System.out.println(" ----> 获取客户端信息开始 <---- ");
getClientInfo();
System.out.println(" ----> 获取客户端信息结束 <---- ");
return"hi," +userName +" welcom to www.micmiu.com";
}
privatevoid getClientInfo(){
try{
MessageContextmc =wsContext.getMessageContext();
HttpExchangeexchange =(HttpExchange)mc
.get(JAXWSProperties.HTTP_EXCHANGE);
InetSocketAddressisa =exchange.getRemoteAddress();
System.out.println("InetSocketAddress : " +isa);
System.out.println("Hostname : "
+isa.getAddress().getHostAddress()+ " address: "
+isa.getAddress().getHostName());
}catch (Exceptione){
e.printStackTrace();
}
}
}
客户端调用后,服务端运行日志:
publishwebservice successful
---->获取客户端信息开始 <----
InetSocketAddress: /127.0.0.1:61333
Hostname :127.0.0.1 address:demo.micmiu.com
---->获取客户端信息结束 <----
从上面的日志信息中可看出:服务端完全可以获取到客户端的IP地址。
2. 情况二:如果以web容器的方式发布(jetty 或 tomcat为例):
服务端接口实现 HelloServiceImpl.java 修改成如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
packagecom.micmiu.jaxws.demo2.impl;
importjavax.annotation.Resource;
import javax.jws.WebService;
importjavax.servlet.http.HttpServletRequest;
import javax.xml.ws.WebServiceContext;
importjavax.xml.ws.handler.MessageContext;
importcom.micmiu.jaxws.demo2.HelloService;
/**
* blog http://www.micmiu.com
*
* @author Michael
*/
@WebService(endpointInterface= "com.micmiu.jaxws.demo2.HelloService")
publicclass HelloServiceImplimplements HelloService{
@Resource
privateWebServiceContext wsContext;
publicString sayHello(StringuserName){
System.out.println(" ----> 获取客户端信息开始 <---- ");
StringclientIP =getClientInfo();
System.out.println(" ----> 获取客户端信息结束 <---- ");
return"Hi," +userName +" welcome to JAX-WS with IP: "+ clientIP
+" . see more http://www.micmiu.com ";
}
privateString getClientInfo(){
StringclientIP =null;
try{
MessageContextmc =wsContext.getMessageContext();
HttpServletRequest request= (HttpServletRequest)(mc
.get(MessageContext.SERVLET_REQUEST));
clientIP= request.getRemoteAddr();
System.out.println("client IP : " +clientIP);
}catch (Exceptione){
e.printStackTrace();
}
returnclientIP;
}
}
客户端代码不用修改,运行如下:
startwebservice client...
send Michael toserver
Hi,Michaelwelcome toJAX-WSwith IP:127.0.0.1 .see more http://www.micmiu.com
test client end.
服务端运行日志如下:
2012-8-619:15:24com.sun.xml.ws.transport.http.servlet.WSServletContextListenercontextInitialized
信息: WSSERVLET12:JAX-WS上下文监听程序正在初始化
2012-8-619:15:25com.sun.xml.ws.transport.http.servlet.WSServletDelegate<init>
信息: WSSERVLET14:JAX-WSservlet 正在初始化
2012-08-0619:15:25.645:INFO::StartedSelectChannelConnector@0.0.0.0:8080
---->获取客户端信息开始 <----
clientIP :127.0.0.1
---->获取客户端信息结束 <----
从上面的日志信息中可看出:服务端完全可以获取到客户端的IP地址。
[二]、基于XFire开发的webservice获取客户端IP地址
以:http://www.micmiu.com/soa/webservice/xfire-ws-base-demo/ 的示例为基础:
服务端接口实现类:HelloServiceImpl.java 修改成如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
packagecom.micmiu.xfire.demo.base;
importjavax.servlet.http.HttpServletRequest;
importorg.codehaus.xfire.transport.http.XFireServletController;
/**
* @blog http://www.micmiu.com
* @author Michael
*/
publicclass HelloWorldServiceImplimplements HelloWorldService{
publicString sayHello(Stringusername){
System.out.println(" ----> 获取客户端信息 <---- ");
StringclientIP =getClientInfo();
System.out.println("客户端IP :" +clientIP);
return"Hi," +username +" welcome,see more http://www.micmiu.com";
}
publicString getClientInfo(){
StringclientIP =null;
try{
HttpServletRequestrequest =XFireServletController.getRequest();
System.out.println("Addr : " +request.getRemoteAddr()+ " host: "
+request.getRemoteHost());
clientIP= request.getRemoteAddr();
}catch (Exceptione){
e.printStackTrace();
}
returnclientIP;
}
}
客户端调用后,服务端的日志信息如下:
---->获取客户端信息 <----
Addr :127.0.0.1 host:127.0.0.1
客户端IP:127.0.0.1
从上面的日志信息中可看出:服务端完全可以获取到客户端的IP地址。
[三]、Axis开发的webservice获取客户端IP地址
以Axis最简单的部署方式为例:
服务端代码:HelloWorld.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
importjavax.servlet.http.HttpServletRequest;
importorg.apache.axis.MessageContext;
import org.apache.axis.transport.http.HTTPConstants;
/**
*
* @author <a href="http://www.micmiu.com">Michael Sun</a>
*/
public classHelloWorld {
publicString sayHello(Stringusername){
System.out.println(" ----> Get client info <---- ");
StringclientIP =getClientInfo();
System.out.println("client ip" +clientIP);
return"Hi," +username +" welcome to axis with IP: "+ clientIP
+" . see more http://www.micmiu.com ";
}
privateString getClientInfo(){
MessageContextmc =null;
HttpServletRequest request= null;
StringclientIP =null;
try{
mc= MessageContext.getCurrentContext();
request= (HttpServletRequest)mc
.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);
clientIP= request.getRemoteAddr();
}catch (Exceptione){
e.printStackTrace();
}
returnclientIP;
}
}
客户端调用代码:Client4Hello.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
importorg.apache.axis.client.Call;
import org.apache.axis.client.Service;
/**
*
* @author <a href="http://www.micmiu.com">Michael Sun</a>
*/
public classClient4Hello {
/**
* @param args
*/
publicstatic voidmain(String[]args){
StringwsdlUrl ="http://localhost:8088/axis/HelloWorld.jws";
try{
System.out.println(" ----> 客户端调用测试 <---- ");
Services =new Service();
Call call= (Call)s.createCall();
call.setOperationName("sayHello");// 这里是要调用的方法名
call.setTargetEndpointAddress(wsdlUrl);// 设置调用的wsdl
Stringret =(String)call.invoke(newObject[]{ "Michael"});
System.out.println("发送 Michael ,返回的信息 :" +ret);
}catch (Exceptione){
e.printStackTrace();
}
}
}
运行客户端结果:
---->客户端调用测试 <----
2012-8-616:56:13org.apache.axis.utils.JavaUtilsisAttachmentSupported
。。。。
发送 Michael,返回的信息 :Hi,Michaelwelcome toaxis with IP:127.0.0.1 .see more http://www.micmiu.com
从tomcat的日志文件catalina.out 中看到服务端运行信息:
---->Get client info <----
client ip127.0.0.1
从上面的日志信息中可看出:服务端完全可以获取到客户端的IP地址。
到此演示了JAX-WS、XFire、Axis三种webservice的获取客户端IP的简单实现过程。