快速业务通道

Servlet过滤器介绍之实用过滤器 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-17
= new OutputStreamWriter(zipOut);       // Compress original output and put it into byte array.       tempOut.write(responseChars);       // Gzip streams must be explicitly closed.       tempOut.close();       // Update the Content-Length header.       res.setContentLength(byteStream.size());       // Send compressed result to client.       OutputStream realOut = res.getOutputStream();       byteStream.writeTo(realOut);     }    }    /**    * Store the FilterConfig object in case subclasses want it.    */    public void init(FilterConfig config) throws ServletException {     this.config = config;    }    protected FilterConfig getFilterConfig() {     return (config);    }    public void destroy() {}    private boolean isGzipSupported(HttpServletRequest req) {     String browserEncodings = req.getHeader("Accept-Encoding");     return ((browserEncodings != null) && (browserEncodings.indexOf ("gzip") != -1));    } }

Servlet过滤器介绍之实用过滤器(8)

时间:2011-04-09 51cto zhangjunhd

LongServlet.java

package com.zj.sample;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet with <B>long</B> output. Used to test the effect of the compression
 * filter of Chapter 9.
 */
 
public class LongServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
           throws ServletException, IOException {
       response.setContentType("text/html");
       PrintWriter out = response.getWriter();
       String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
              + "Transitional//EN\">\n";
       String title = "Long Page";
       out.println(docType + "<HTML>\n" + "<HEAD><TITLE>" + title
              + "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n"
              + "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n");
       String line = "Blah, blah, blah, blah, blah. "
              + "Yadda, yadda, yadda, yadda.";
       for (int i = 0; i < 10000; i++) {
           out.println(line);
       }
       out.println("</BODY></HTML>");
    }
}

web.xml

<filter>    <filter-name>CompressionFilter</filter-name>    <filter-class>com.zj.sample.CompressionFilter</filter-class> </filter> <filter-mapping>    <filter-name>CompressionFilter</filter-name>    <servlet-name>LongServlet</servlet-name> </filter-mapping> <servlet>    <servlet-name>LongServlet</servlet-name&g

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号