我有一种使用HttpServletRequest
,HttpServletResponse
和ServletContext
的方法。
这是我的代码:
public String processarModelo(MyObject ou, Map<String, Object> attrs,
Map<String, Object> params) throws Exception {
ServletContext sc = com.opensymphony.webwork.ServletActionContext.getServletContext();
HttpServletResponse r = com.opensymphony.webwork.ServletActionContext.getResponse();
MyHttpRequest rw = new MyHttpRequest(com.opensymphony.webwork.ServletActionContext.getRequest());
rw.clearAttributes();
for (String s : attrs.keySet()) {
rw.setAttribute(s, attrs.get(s));
}
Map<String, String> p = rw.getParameterMap();
p.clear();
for (String s : params.keySet()) {
p.put(s, (String) params.get(s));
}
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Writer w = new OutputStreamWriter(bout);
SwallowingHttpServletResponse r2 = new SwallowingHttpServletResponse(r, w, "iso-8859-1");
javax.servlet.RequestDispatcher dispatcher = sc.getRequestDispatcher("/paginas/expediente/processa_modelo.jsp");
dispatcher.include(rw, r2);
w.flush();
String s = bout.toString();
return s;
}
当我从浏览器中调用它时,它正在工作。
本周,我构建了一个必须调用相同方法的Web服务。但是在webservice方法内部,我手边没有
ServletContext
,HttpServletResponse
和HttpServletRequest
。如何模拟它们以呈现我的JSP并获取生成的HTML?
最佳答案
好吧,在@Context注释周围搜索时,我发现了这个用于Web服务的内容:
@Resource
private WebServiceContext context;
有了这个,我可以得到我需要的一切:
HttpServletRequest request = (HttpServletRequest)context.getMessageContext().get(MessageContext.SERVLET_REQUEST);
ServletContext servletContext = (ServletContext) context.getMessageContext().get(MessageContext.SERVLET_CONTEXT);
HttpServletResponse response = (HttpServletResponse) context.getMessageContext().get(MessageContext.SERVLET_RESPONSE);
谢谢大家的帮助!
关于java - 如何模拟HttpServletRequest,HttpServletResponse和ServletContext?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14443990/