如何在Java中将HttpServletResponse作为对象访问

时间:2018-11-12 11:13:12

标签: java servlets response

我尝试了下面的代码,但得到null,请提供其他任何方式将HttpServletResponse作为对象来处理。

ServletWebRequest servletWebRequest=new ServletWebRequest(request);
HttpServletResponse response=servletWebRequest.getResponse();

1 个答案:

答案 0 :(得分:0)

尝试使用WebServiceContext

  

WebServiceContext接口是JAX-WS 2.2规范第5.3节中介绍的服务API之一。为了使用Web服务上下文,请使用javax.annotation.Resource注释注入资源。

示例:

@WebService
public class AddNumbersImpl {
    // Inject the information about the request that is currently being processed in the wsContext field
    @Resource
    private WebServiceContext wsContext;

    public int add(int number1, int number2) throws AddNumbersFault {
        // Acquire the message context
        MessageContext mContext = wsContext.getMessageContext();
        // Acquire the properties
        ServletContext sContext = (ServletContext)mContext.get(MessageContext.SERVLET_CONTEXT);

对于HttpServletResponse,请使用:

HttpServletResponse response = (HttpServletResponse) mContext.getMessageContext().get(MessageContext.SERVLET_RESPONSE);