有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

servlets如何在java中作为对象访问HttpServletResponse

我尝试了下面的代码,结果为空,请提供任何其他方法将HttpServletResponse作为对象处理

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

共 (1) 个答案

  1. # 1 楼答案

    尝试使用WebServiceContext

    WebServiceContext interface is one of the service APIs explained in the JAX-WS 2.2 specifications, section 5.3. For using the Web services context, inject a resource by using the javax.annotation.Resource annotation.

    例如:

    @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);