简体   繁体   中英

How can i access HttpServletResponse as an object in java

I have tried below code and got null, Please provide any other way to handle HttpServletResponse as object.

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

Try using 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.

Example:

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

And for HttpServletResponse use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM