您的位置:首页 > 编程语言 > Java开发

jax-ws 获取客户端相关信息

2016-11-24 15:02 218 查看
本博客转自 http://blog.csdn.net/z69183787/article/details/46884165, 尊重原创

基于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 修改如下:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051package com.micmiu.jaxws.demo.impl; import java.net.InetSocketAddress; import javax.annotation.Resource;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;import javax.xml.ws.WebServiceContext;import javax.xml.ws.handler.MessageContext; import com.micmiu.jaxws.demo.HelloService;import com.sun.net.httpserver.HttpExchange;import com.sun.xml.internal.ws.developer.JAXWSProperties; /** * blog http://www.micmiu.com * * @author Michael */@WebService()public class HelloServiceImpl implements HelloService { @Resource private WebServiceContext wsContext; @WebMethod public String sayHello(@WebParam(name = "userName") String userName) { System.out.println(" ----> 获取客户端信息开始 <---- "); getClientInfo(); System.out.println(" ----> 获取客户端信息结束 <---- "); return "hi," + userName + " welcom to www.micmiu.com"; } private void getClientInfo() { try { MessageContext mc = wsContext.getMessageContext(); HttpExchange exchange = (HttpExchange) mc .get(JAXWSProperties.HTTP_EXCHANGE); InetSocketAddress isa = exchange.getRemoteAddress(); System.out.println("InetSocketAddress : " + isa); System.out.println("Hostname : " + isa.getAddress().getHostAddress() + " address: " + isa.getAddress().getHostName()); } catch (Exception e) { e.printStackTrace(); } }}
客户端调用后,服务端运行日志:

1

2

3

4

5

publishwebservicesuccessful

---->获取客户端信息开始<----

InetSocketAddress:/127.0.0.1:61333

Hostname:127.0.0.1address:demo.micmiu.com

---->获取客户端信息结束<----

从上面的日志信息中可看出:服务端完全可以获取到客户端的IP地址。

2. 情况二:如果以web容器的方式发布(jetty 或 tomcat为例):

服务端接口实现 HelloServiceImpl.java 修改成如下:

123456789101112131415161718192021222324252627282930313233343536373839404142434445package com.micmiu.jaxws.demo2.impl; import javax.annotation.Resource;import javax.jws.WebService;import javax.servlet.http.HttpServletRequest;import javax.xml.ws.WebServiceContext;import javax.xml.ws.handler.MessageContext; import com.micmiu.jaxws.demo2.HelloService; /** * blog http://www.micmiu.com * * @author Michael */@WebService(endpointInterface = "com.micmiu.jaxws.demo2.HelloService")public class HelloServiceImpl implements HelloService { @Resource private WebServiceContext wsContext; public String sayHello(String userName) { System.out.println(" ----> 获取客户端信息开始 <---- "); String clientIP = getClientInfo(); System.out.println(" ----> 获取客户端信息结束 <---- "); return "Hi," + userName + " welcome to JAX-WS with IP: " + clientIP + " . see more http://www.micmiu.com "; } private String getClientInfo() { String clientIP = null; try { MessageContext mc = wsContext.getMessageContext(); HttpServletRequest request = (HttpServletRequest) (mc .get(MessageContext.SERVLET_REQUEST)); clientIP = request.getRemoteAddr(); System.out.println("client IP : " + clientIP); } catch (Exception e) { e.printStackTrace(); } return clientIP; }}
客户端代码不用修改,运行如下:

1

2

3

4

startwebserviceclient...

sendMichaeltoserver

Hi,MichaelwelcometoJAX-WSwithIP:127.0.0.1.seemorehttp://www.micmiu.com

testclientend.

服务端运行日志如下:

123456782012-8-6 19:15:24 com.sun.xml.ws.transport.http.servlet.WSServletContextListener contextInitialized信息: WSSERVLET12: JAX-WS 上下文监听程序正在初始化2012-8-6 19:15:25 com.sun.xml.ws.transport.http.servlet.WSServletDelegate <init>信息: WSSERVLET14: JAX-WS servlet 正在初始化2012-08-06 19:15:25.645:INFO::Started SelectChannelConnector@0.0.0.0:8080 ----> 获取客户端信息开始 <----client IP : 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

*/

publicclassHelloWorldServiceImplimplementsHelloWorldService{

publicStringsayHello(Stringusername){

System.out.println("
----> 获取客户端信息 <---- ");

StringclientIP=getClientInfo();

System.out.println("客户端IP
:"+clientIP);

return"Hi,"+username+"
welcome,see more http://www.micmiu.com";
}

publicStringgetClientInfo(){

StringclientIP=null;

try{

HttpServletRequestrequest=XFireServletController.getRequest();

System.out.println("Addr
: "+request.getRemoteAddr()+"
host: "

+request.getRemoteHost());

clientIP=request.getRemoteAddr();

}catch(Exceptione){

e.printStackTrace();

}

returnclientIP;

}

}

客户端调用后,服务端的日志信息如下:

123----> 获取客户端信息 <----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;

importorg.apache.axis.transport.http.HTTPConstants;

/**

*

* @author <a href="http://www.micmiu.com">Michael
Sun</a>

*/

publicclassHelloWorld{

publicStringsayHello(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 ";

}

privateStringgetClientInfo(){

MessageContextmc=null;

HttpServletRequestrequest=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

123456789101112131415161718192021222324252627282930import org.apache.axis.client.Call;import org.apache.axis.client.Service; /** * * @author <a href="http://www.micmiu.com">Michael Sun</a> */public class Client4Hello { /** * @param args */ public static void main(String[] args) { String wsdlUrl = "http://localhost:8088/axis/HelloWorld.jws"; try { System.out.println(" ----> 客户端调用测试 <---- "); Service s = new Service(); Call call = (Call) s.createCall(); call.setOperationName("sayHello");// 这里是要调用的方法名 call.setTargetEndpointAddress(wsdlUrl);// 设置调用的wsdl String ret = (String) call.invoke(new Object[] { "Michael" }); System.out.println("发送 Michael ,返回的信息 :" + ret); } catch (Exception e) { e.printStackTrace(); } } }
运行客户端结果:

1

2

3

4

---->客户端调用测试 <----

2012-8-616:56:13org.apache.axis.utils.JavaUtilsisAttachmentSupported

。。。。

发送Michael,返回的信息:Hi,MichaelwelcometoaxiswithIP:127.0.0.1.seemorehttp://www.micmiu.com

从tomcat的日志文件catalina.out 中看到服务端运行信息:

1

2

---->Getclientinfo <----

clientip127.0.0.1

从上面的日志信息中可看出:服务端完全可以获取到客户端的IP地址。

到此演示了JAX-WS、XFire、Axis三种webservice的获取客户端IP的简单实现过程。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java jdk web service 博客