我如何使用Java Web服务时,可以访问HttpServletRequest对象 [英] How can I get access to the HttpServletRequest object when using Java Web Services

查看:262
本文介绍了我如何使用Java Web服务时,可以访问HttpServletRequest对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java 6中,Tomcat的6,和地铁。我使用的WebService的WebMethod和注释揭露我的web服务。我想获得有关请求的信息。我尝试以下code,但wsCtxt始终为空。我必须采取什么步骤的的获取空的WebServiceContext。

在换句话说:我怎么能执行以下命令行来获得一个非空值wsCtxt

MessageContext的msgCtxt = wsCtxt.getMessageContext();

  @WebService
公共类为MyService {  @Resource
  WebServiceContext wsCtxt;  @WebMethod
  公共无效myWebMethod(){
    MessageContext的msgCtxt = wsCtxt.getMessageContext();
    HttpServletRequest的REQ =(HttpServletRequest的)msgCtxt.get(MessageContext.SERVLET_REQUEST);
    串clientIP = req.getRemoteAddr();
  }


解决方案

我建议你无论是从wsCtxt您的变量重命名为wsContext或指定的name属性@Resource注解。在@Resource的 J2ee的教程表明该变量的名字被用作查找的一部分。我一直在使用资源注入GlassFish中注入不同类型的资源遇到了这个相同的问题。

虽然你的正确名称不得wsContext。我下面这个<一个href=\"http://www.java-tips.org/java-ee-tips/java-api-for-xml-web-services/using-jax-ws-based-web-services-wit.html\">java小费。如果你喜欢的变量名wsCtxt,然后使用name属性变量声明:


  

@资源(名称=wsContext)WebServiceContext wsCtxt;


I'm using Java 6, Tomcat 6, and Metro. I use WebService and WebMethod annotations to expose my web service. I would like to obtain information about the request. I tried the following code, but wsCtxt is always null. What step must I take to not get null for the WebServiceContext.

In other words: how can I execute the following line to get a non-null value for wsCtxt?

MessageContext msgCtxt = wsCtxt.getMessageContext();

@WebService
public class MyService{

  @Resource
  WebServiceContext wsCtxt;

  @WebMethod
  public void myWebMethod(){
    MessageContext msgCtxt = wsCtxt.getMessageContext();
    HttpServletRequest req = (HttpServletRequest)msgCtxt.get(MessageContext.SERVLET_REQUEST);
    String clientIP = req.getRemoteAddr();
  }

解决方案

I recommend you either rename your variable from wsCtxt to wsContext or assign the name attribute to the @Resource annotation. The J2ee tutorial on @Resource indicates that the name of the variable is used as part of the lookup. I've encountered this same problem using resource injection in Glassfish injecting a different type of resource.

Though your correct name may not be wsContext. I'm following this java tip. If you like the variable name wsCtxt, then use the name attribute in the variable declaration:

@Resource(name="wsContext") WebServiceContext wsCtxt;

这篇关于我如何使用Java Web服务时,可以访问HttpServletRequest对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆