问题描述
JavaEE API附带了HttpServletResponseWrapper,引用javadoc,提供了一个方便的HttpServletResponse接口实现,可以由希望调整Servlet响应的开发人员进行子类化。没有子类化,这个类只是将所有调用传递给包装的响应对象。请求有一个类似的包装器。
The JavaEE API comes with the HttpServletResponseWrapper which, to quote the javadoc, "provides a convenient implementation of the HttpServletResponse interface that can be subclassed by developers wishing to adapt the response from a Servlet." Without subclassing, this class just passes all calls through to the wrapped response object. There's a similar wrapper for requests.
任何人都可以指向任何提供这些类的有用子类实现的实用程序库吗?特别是,我正在寻找响应包装器的子类,它捕获写入的响应(作为String,byte [],无论什么是合适的)并通过API方法公开它。
Can anyone point me at any utility libraries that provide useful subclass implementations of these classes? Particularly, I'm looking for a subclass of the response wrapper that captures the written response (as a String, byte[], whatever's appropriate) and exposes it via an API method.
我自己写了一个相当半生不熟的版本,但坦率地说,我不应该这样做,我宁愿扔掉它并使用一个现成的。
I've written a rather half-baked version myself, but frankly, I shouldn't have to, and I'd rather throw it away and use one off the shelf.
推荐答案
我不知道任何实现,即使只需写入ByteArrayOutputStream就可以轻松调整gzip示例。您还可以通过查看其他响应包装器实现来获取想法:
I am not aware of any implementation, even though the gzip example can be adapted easily by just writing to a ByteArrayOutputStream. You can also take ideas by looking at other response wrapper implementations at:
- 。您可以找到包装请求和响应的示例:
There is the classic article in JavaWorld Filter code with Servlet 2.3 model. You can find examples for wrapped request and response:
public class CompressionResponseWrapper extends HttpServletResponseWrapper { protected ServletOutputStream stream = null; protected PrintWriter writer = null; protected int threshold = 0; protected HttpServletResponse origResponse = null; public CompressionResponseWrapper(HttpServletResponse response) { super(response); origResponse = response; } public void setCompressionThreshold(int threshold) { this.threshold = threshold; } public ServletOutputStream createOutputStream() throws IOException { return (new CompressionResponseStream(origResponse)); } public ServletOutputStream getOutputStream() throws IOException { if (writer != null) { throw new IllegalStateException("getWriter() has already been " + "called for this response"); } if (stream == null) { stream = createOutputStream(); } ((CompressionResponseStream) stream).setCommit(true); ((CompressionResponseStream) stream).setBuffer(threshold); return stream; } public PrintWriter getWriter() throws IOException { if (writer != null) { return writer; } if (stream != null) { throw new IllegalStateException("getOutputStream() has already " + "been called for this response"); } stream = createOutputStream(); ((CompressionResponseStream) stream).setCommit(true); ((CompressionResponseStream) stream).setBuffer(threshold); writer = new PrintWriter(stream); return writer; } }
-
-
public class MultipartWrapper extends HttpServletRequestWrapper { MultipartRequest mreq = null; public MultipartWrapper(HttpServletRequest req, String dir) throws IOException { super(req); mreq = new MultipartRequest(req, dir); } // Methods to replace HSR methods public Enumeration getParameterNames() { return mreq.getParameterNames(); } public String getParameter(String name) { return mreq.getParameter(name); } public String[] getParameterValues(String name) { return mreq.getParameterValues(name); } public Map getParameterMap() { Map map = new HashMap(); Enumeration enum = getParameterNames(); while (enum.hasMoreElements()) { String name = (String) enum.nextElement(); map.put(name, mreq.getParameterValues(name)); } return map; } // Methods only in MultipartRequest public Enumeration getFileNames() { return mreq.getFileNames(); } public String getFilesystemName(String name) { return mreq.getFilesystemName(name); } public String getContentType(String name) { return mreq.getContentType(name); } public File getFile(String name) { return mreq.getFile(name); } }
代码有点陈旧(2001年6月!),但它很好地证明了用法。
The code is a bit old (june 2001!), but it demonstrate the usage well.
这篇关于寻找HttpServletResponseWrapper的捕获impl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!