使用 Java Web 服务时如何访问 HttpServletRequest 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/133436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:41:07  来源:igfitidea点击:

How can I get access to the HttpServletRequest object when using Java Web Services

javaweb-servicesannotationsservlets

提问by Steve McLeod

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 notget null for the WebServiceContext.

我正在使用 Java 6、Tomcat 6 和 Metro。我使用 WebService 和 WebMethod 注释来公开我的 Web 服务。我想获取有关请求的信息。我尝试了以下代码,但 wsCtxt 始终为空。我必须采取什么步骤才能使 WebServiceContext为 null。

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

换句话说:如何执行以下行来获取 wsCtxt 的非空值?

MessageContext msgCtxt = wsCtxt.getMessageContext();

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();
  }

采纳答案by James A Wilson

I recommend you either rename your variable from wsCtxt to wsContext or assign the name attribute to the @Resource annotation. The J2ee tutorial on @Resourceindicates 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.

我建议您将变量从 wsCtxt 重命名为 wsContext 或将 name 属性分配给 @Resource 注释。@Resource 上J2ee 教程指出变量的名称用作查找的一部分。我在 Glassfish 中使用资源注入来注入不同类型的资源时遇到了同样的问题。

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:

尽管您的正确名称可能不是 wsContext。我正在关注这个java 技巧。如果你喜欢变量名 wsCtxt,那么在变量声明中使用 name 属性:

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

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

回答by asterite

Maybe the javax.ws.rs.core.Context annotation is for what you are looking for, instead of Resource?

也许 javax.ws.rs.core.Context 注释是针对您要查找的内容,而不是资源?

回答by Steve McLeod

I still have this problem. Here is my work-around was to write a ServletRequestListener that puts the request into a ThreadLocal var. Then the WebService can obtain the request from the ThreadLocal. In other words, I'm reimplementing something that just doesn't work for me.

我仍然有这个问题。这是我的解决方法是编写一个 ServletRequestListener 将请求放入 ThreadLocal var。然后 WebService 就可以从 ThreadLocal 获取请求。换句话说,我正在重新实现一些对我不起作用的东西。

Here's the Listener:

这是监听器:

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;

public class SDMXRequestListener implements ServletRequestListener {

    public SDMXRequestListener() {
    }

    public void requestDestroyed(ServletRequestEvent event) {
    }

    public void requestInitialized(ServletRequestEvent event) {
        final ServletRequest request = event.getServletRequest();
        ServletRequestStore.setServletRequest(request);
    }

}

Here's the ThreadLocal wrapper:

这是 ThreadLocal 包装器:

import javax.servlet.ServletRequest;

public class ServletRequestStore {

    private final static ThreadLocal<ServletRequest> servletRequests = new ThreadLocal<ServletRequest>();

    public static void setServletRequest(ServletRequest request) {
        servletRequests.set(request);
    }

    public static ServletRequest getServletRequest() {
        return servletRequests.get();
    }

}

And the web.xml wiring:

和 web.xml 接线:

  <listener>
        <listener-class>ecb.sdw.webservices.SDMXRequestListener</listener-class>
    </listener>

The Web service uses the following code to obtain the request:

Web 服务使用以下代码获取请求:

final HttpServletRequest request = (HttpServletRequest) ServletRequestStore.getServletRequest();

最终 HttpServletRequest 请求 = (HttpServletRequest) ServletRequestStore.getServletRequest();

回答by Garth Gilmour

The following code works for me using Java 5, Tomcat 6 and Metro

以下代码适用于我使用 Java 5、Tomcat 6 和 Metro

Could it possibly be that there is a conflict between the WS support in Java 6 and the version of Metro you are using. Have you tried it on a Java 5 build?

是否可能是 Java 6 中的 WS 支持与您使用的 Metro 版本之间存在冲突。您是否在 Java 5 版本上尝试过?

@WebService
public class Sample {
    @WebMethod
    public void sample() {
        HttpSession session = findSession();
        //Stuff

    }
    private HttpSession findSession() {
        MessageContext mc = wsContext.getMessageContext();
        HttpServletRequest request = (HttpServletRequest)mc.get(MessageContext.SERVLET_REQUEST);
        return request.getSession();
    }
    @Resource
    private WebServiceContext wsContext;
}