我在下面的一段程序中看到的:
public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain )
throws IOException, ServletException { request.setCharacterEncoding("utf-8");
if (compressionThreshold == 0) {
chain.doFilter(request, response);
return;
}
boolean supportCompression = false;
if (request instanceof HttpServletRequest) { // Are we allowed to compress ?
String s = request.getParameter("gzip");
if ("false".equals(s)) {
chain.doFilter(request, response);
return;
} Enumeration e =
((HttpServletRequest)request).getHeaders("Accept-Encoding");
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
if (name.indexOf("gzip") != -1) {
//log.trace("supports compression");
supportCompression = true;
} else {
//log.trace("no support for compresion");
}
}
} if (!supportCompression) {
//log.trace("doFilter gets called wo compression");
chain.doFilter(request, response);
return;
} else {
if (response instanceof HttpServletResponse) {
CompressionServletResponseWrapper wrappedResponse =
new CompressionServletResponseWrapper((HttpServletResponse)response);
//minhnn
//wrappedResponse.setDebugLevel(debug);
wrappedResponse.setCompressionThreshold(compressionThreshold);
//log.trace("doFilter gets called with compression");
try {
chain.doFilter(request, wrappedResponse);
} finally {
wrappedResponse.finishResponse();
}
return;
}
}
}
其中CompressionServletResponseWrapper 继承了httpservletresponsewrapper,这么做有什么意义呢?
public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain )
throws IOException, ServletException { request.setCharacterEncoding("utf-8");
if (compressionThreshold == 0) {
chain.doFilter(request, response);
return;
}
boolean supportCompression = false;
if (request instanceof HttpServletRequest) { // Are we allowed to compress ?
String s = request.getParameter("gzip");
if ("false".equals(s)) {
chain.doFilter(request, response);
return;
} Enumeration e =
((HttpServletRequest)request).getHeaders("Accept-Encoding");
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
if (name.indexOf("gzip") != -1) {
//log.trace("supports compression");
supportCompression = true;
} else {
//log.trace("no support for compresion");
}
}
} if (!supportCompression) {
//log.trace("doFilter gets called wo compression");
chain.doFilter(request, response);
return;
} else {
if (response instanceof HttpServletResponse) {
CompressionServletResponseWrapper wrappedResponse =
new CompressionServletResponseWrapper((HttpServletResponse)response);
//minhnn
//wrappedResponse.setDebugLevel(debug);
wrappedResponse.setCompressionThreshold(compressionThreshold);
//log.trace("doFilter gets called with compression");
try {
chain.doFilter(request, wrappedResponse);
} finally {
wrappedResponse.finishResponse();
}
return;
}
}
}
其中CompressionServletResponseWrapper 继承了httpservletresponsewrapper,这么做有什么意义呢?
解决方案 »
- Js获取<select>中option 的值...急!
- 求助!!
- 如何获取全部request?
- jsp访问SQL server2000出现的问题
- 使用javabean到底需要什么环境,
- 请问一下jdbc直连和数据源的区别和联系,再问一下它们的性能以及如何选择?
- 求救!!!使用JACOB将EXCEL转换为.HTM出错。
- 在测试crawler4j的controller类时抛出NoClassDefFoundError这个异常,该怎么解决?
- 通过文件浏览选择excel文件读取其中的数据存入数据库
- 让我吐血的问题,要疯掉了!有代码……在线等大家帮忙!!
- 有人知道有JSP的自助建站系统吗?
- 入门问题:如何在JSP中建立一个frame??
public class CompressionServletResponseWrapper extends
HttpServletResponseWrapper { // minhnn : I remove the log because log4j does not support TRACE level
private static Log log = LogFactory
.getLog(CompressionServletResponseWrapper.class); // ----------------------------------------------------- Constructor /**
* Calls the parent constructor which creates a ServletResponse adaptor
* wrapping the given response object.
*/ public CompressionServletResponseWrapper(HttpServletResponse response) {
super(response);
origResponse = response;
} // ----------------------------------------------------- Instance Variables /**
* Original response
*/ protected HttpServletResponse origResponse = null; /**
* Descriptive information about this Response implementation.
*/ protected static final String info = "CompressionServletResponseWrapper"; /**
* The ServletOutputStream that has been returned by
* <code>getOutputStream()</code>, if any.
*/ protected ServletOutputStream stream = null; /**
* The PrintWriter that has been returned by <code>getWriter()</code>, if
* any.
*/ protected PrintWriter writer = null; /**
* The threshold number to compress
*/
protected int threshold = 0; /**
* Content type
*/
protected String contentType = null; // --------------------------------------------------------- Public Methods /**
* Set content type
*/
public void setContentType(String contentType) {
this.contentType = contentType;
origResponse.setContentType(contentType);
} /**
* Set threshold number
*/
public void setCompressionThreshold(int threshold) {
this.threshold = threshold;
} /**
* Create and return a ServletOutputStream to write the content associated
* with this Response.
*
* @exception IOException
* if an input/output error occurs
*/
public ServletOutputStream createOutputStream() throws IOException {
// log.trace("createOutputStream gets called"); CompressionResponseStream stream = new CompressionResponseStream(
origResponse);
// stream.setDebugLevel(debug);
stream.setBuffer(threshold); return stream; } /**
* Finish a response.
*/
public void finishResponse() {
try {
if (writer != null) {
writer.close();
} else {
if (stream != null)
stream.close();
}
} catch (IOException e) {
}
} // ------------------------------------------------ ServletResponse Methods /**
* Flush the buffer and commit this response.
*
* @exception IOException
* if an input/output error occurs
*/
public void flushBuffer() throws IOException { if (stream != null) {
// minhnn @todo : how to ensure the stream still open ???
((CompressionResponseStream) stream).flush();
} } /**
* Return the servlet output stream associated with this Response.
*
* @exception IllegalStateException
* if <code>getWriter</code> has already been called for
* this response
* @exception IOException
* if an input/output error occurs
*/
public ServletOutputStream getOutputStream() throws IOException { if (writer != null)
throw new IllegalStateException(
"getWriter() has already been called for this response"); if (stream == null) {
stream = createOutputStream();
}
// log.trace("stream is set to " + stream + " in getOutputStream"); return (stream); } /**
* Return the writer associated with this Response.
*
* @exception IllegalStateException
* if <code>getOutputStream</code> has already been called
* for this response
* @exception IOException
* if an input/output error occurs
*/
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();
String charEnc = origResponse.getCharacterEncoding();
if (charEnc != null) {
writer = new PrintWriter(new OutputStreamWriter(stream, charEnc));
} else {
writer = new PrintWriter(stream);
} return (writer); } public void setContentLength(int length) {
} /**
* Returns character from content type. This method was taken from tomcat.
*
* @author rajo
*/
private static String getCharsetFromContentType(String type) { if (type == null) {
return null;
}
int semi = type.indexOf(";");
if (semi == -1) {
return null;
}
String afterSemi = type.substring(semi + 1);
int charsetLocation = afterSemi.indexOf("charset=");
if (charsetLocation == -1) {
return null;
} else {
String afterCharset = afterSemi.substring(charsetLocation + 8);
String encoding = afterCharset.trim();
return encoding;
}
}}