• webservice服务器端获取request对象的三种方式


      有的时候在webservice里我们需要获取request对象和response对象,比如想要获得客户端的访问ip的时候就需要这么做,下面说三种方式,当然三种方式可能是针对不同方式部署webservice获取request对象的方法。

    第一种:先配置注入:

    @Resource
    private WebServiceContext webServiceContext;

    其次是下面的代码:

    MessageContext mc = webServiceContext.getMessageContext();
    HttpServletRequest request = (HttpServletRequest) (mc.get(MessageContext.SERVLET_REQUEST));

    第二种:

    WebServiceContext context = new WebServiceContextImpl();
    MessageContext ctx = context.getMessageContext();
    HttpServletRequest request = (HttpServletRequest) ctx.get(AbstractHTTPDestination.HTTP_REQUEST);

    第三种(附带获取客户端ip地址的方法):

    Message message = PhaseInterceptorChain.getCurrentMessage();
    HttpServletRequest request = (HttpServletRequest) message.get(AbstractHTTPDestination.HTTP_REQUEST);

    获取ip:

    复制代码
    public static String getIpAddr(HttpServletRequest request) {
    String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknow".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return ip;
        }
    复制代码
  • 相关阅读:
    timeDate.js 插件优化
    向页面中插入不同格式的时间(timeDate.js)
    html
    html
    html
    html
    three.js
    three.js
    three.js
    python之路_头像预览、each循环及form组件校验验证码
  • 原文地址:https://www.cnblogs.com/soul-wonder/p/8889108.html
Copyright © 2020-2023  润新知