HTTP 压缩是指浏览器能够理解压缩过的 HTML 代码, 文本文档等, 从而减少网络传输流量的一种功能. 在目前的大部分主流浏览器上, 都支持这种功能, 例如 Ineternet Explorer, FireFox, Netscape, Opera 等, 对应的 HTTP 版本应该是 HTTP 1.1.
要探测浏览器是否支持 HTTP 压缩, 只要查看浏览器请求的头中是否有如下字样:
Accept-Encoding: gzip, deflate
即可, 这个即是说明浏览器支持 gzip 和 deflate(一种压缩格式)的压缩内容. 这就意味着: 可以向浏览器返回默认的未压缩的文本, 也可以用 GZIP 和 deflate 压缩过后传送给它, 浏览器会自己探测这些内容并解压缩, 这就以为着传输某些资料的时候(例如文本的 HTML 代码)可以大大减少网络需要传送的内容数.
那么, 如何实现这个功能呢? 其实 Apache Tomcat 5 安装时候所自带的示例目录下都做好了一个过滤器用来实现这个功能了:
{Tomcat5.0安装目录}\webapps\jsp-examples\WEB-INF\classes\compressionFilters
下就是这个过滤器的源码和类文件, 使用时候只要将这些文件放入类路径中, 然后向 web.xml 中加入过滤器配置语句就可以了:
要探测浏览器是否支持 HTTP 压缩, 只要查看浏览器请求的头中是否有如下字样:
Accept-Encoding: gzip, deflate
即可, 这个即是说明浏览器支持 gzip 和 deflate(一种压缩格式)的压缩内容. 这就意味着: 可以向浏览器返回默认的未压缩的文本, 也可以用 GZIP 和 deflate 压缩过后传送给它, 浏览器会自己探测这些内容并解压缩, 这就以为着传输某些资料的时候(例如文本的 HTML 代码)可以大大减少网络需要传送的内容数.
那么, 如何实现这个功能呢? 其实 Apache Tomcat 5 安装时候所自带的示例目录下都做好了一个过滤器用来实现这个功能了:
{Tomcat5.0安装目录}\webapps\jsp-examples\WEB-INF\classes\compressionFilters
下就是这个过滤器的源码和类文件, 使用时候只要将这些文件放入类路径中, 然后向 web.xml 中加入过滤器配置语句就可以了:
这个小实例使用GZIP压缩的方式回传给浏览器,而IE5和NS6也都有支持Gzip的压缩格式。
这个方法在之前就有人提出过,因为是让网页在输出时经过压缩,可以让传输量变小很多,虽然现在的网络频宽对于用来看网页已经绰绰有余,但是档案大小太大的网页还是会造成一定的影响。
这里是压缩功能的代码:
这个方法在之前就有人提出过,因为是让网页在输出时经过压缩,可以让传输量变小很多,虽然现在的网络频宽对于用来看网页已经绰绰有余,但是档案大小太大的网页还是会造成一定的影响。
这里是压缩功能的代码:
复制内容到剪贴板 程序代码
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;
拳头是最基本的一重武器,也是最重要的,好比练武之人必须先扎稳马步。
2.孔雀翎之支持https
复制内容到剪贴板 程序代码
HttpClient httpclient=new HttpClient();//创建一个客户端,类似打开一个浏览器
GetMethod getMethod=new GetMethod("http://www.blablabla.com");//创建一个get方法,类似在浏览器地址栏中输入一个地址
int statusCode=httpclient.executeMethod(getMethod);//回车——出拳!
System.out.println("response=" + getMethod.getResponseBodyAsString());//察看拳头命中情况,可以获得的东西还有很多,比如head, cookies等等
getMethod.releaseConnection();//释放,记得收拳哦
GetMethod getMethod=new GetMethod("http://www.blablabla.com");//创建一个get方法,类似在浏览器地址栏中输入一个地址
int statusCode=httpclient.executeMethod(getMethod);//回车——出拳!
System.out.println("response=" + getMethod.getResponseBodyAsString());//察看拳头命中情况,可以获得的东西还有很多,比如head, cookies等等
getMethod.releaseConnection();//释放,记得收拳哦
2.孔雀翎之支持https
Tags: HttpClient 武器
说句实话,JCL(Jakarta Commons Logging)和log4j真把我搞蒙了。不都是做log的吗,怎么在jcl的源码包中,还有个log4j的包?倒底谁跟谁啊?至到看了jcl的用户指南,才明白一些。hehe.
1、Commons-Loggin简介
Jakarta Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具体的日志实现工具。 它提供给中间件/日志工具开发者一个简单的日志操作抽象,允许程序开发人员使用不同的具体日志实现工具。用户被假定已熟悉某种日志实现工具的更高级别的细节。JCL提供的接口,对其它一些日志工具,包括Log4J, Avalon LogKit, and JDK 1.4等,进行了简单的包装,此接口更接近于Log4J和LogKit的实现.
2、快速入门
1、Commons-Loggin简介
Jakarta Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具体的日志实现工具。 它提供给中间件/日志工具开发者一个简单的日志操作抽象,允许程序开发人员使用不同的具体日志实现工具。用户被假定已熟悉某种日志实现工具的更高级别的细节。JCL提供的接口,对其它一些日志工具,包括Log4J, Avalon LogKit, and JDK 1.4等,进行了简单的包装,此接口更接近于Log4J和LogKit的实现.
2、快速入门
《每一个WEB应用程序,都应该有两个过滤器》by Jayson Falkner
原文如下:
Almost every single web application you will ever make will seriously benefit from using servlet filters to both cache and compress content. A caching filter optimizes the time it takes to send back a response from your web server, and a compression filter optimizes the size of the content that you send from your web server to a user via the Internet. Since generating content and sending content over the World Wide Web are the bread and butter of web applications, it should be no surprise that simple components that aid in these processes are incredibly useful. This article details the process of building and using a caching filter and a compression filter that are suitable for use with just about any web application. After reading this article, you will understand caching and compressing, have code to do both, and be able to apply caching and compression to any of your future (or existing!) web applications.
Review: Servlet Filters in 10 Sentences
Servlet filters are powerful tools that are available to web application developers using the Servlet 2.3 specification or above. Filters are designed to be able to manipulate a request or response (or both) that is sent to a web application, yet provide this functionality in a method that won't affect servlets and JSPs being used by the web application unless that is the desired effect. A good way to think of servlet filters is as a chain of steps that a request and response must go through before reaching a servlet, JSP, or static resource such as an HTML page in a web application. Figure 1 shows the commonly used illustration of this concept.
原文如下:
Almost every single web application you will ever make will seriously benefit from using servlet filters to both cache and compress content. A caching filter optimizes the time it takes to send back a response from your web server, and a compression filter optimizes the size of the content that you send from your web server to a user via the Internet. Since generating content and sending content over the World Wide Web are the bread and butter of web applications, it should be no surprise that simple components that aid in these processes are incredibly useful. This article details the process of building and using a caching filter and a compression filter that are suitable for use with just about any web application. After reading this article, you will understand caching and compressing, have code to do both, and be able to apply caching and compression to any of your future (or existing!) web applications.
Review: Servlet Filters in 10 Sentences
Servlet filters are powerful tools that are available to web application developers using the Servlet 2.3 specification or above. Filters are designed to be able to manipulate a request or response (or both) that is sent to a web application, yet provide this functionality in a method that won't affect servlets and JSPs being used by the web application unless that is the desired effect. A good way to think of servlet filters is as a chain of steps that a request and response must go through before reaching a servlet, JSP, or static resource such as an HTML page in a web application. Figure 1 shows the commonly used illustration of this concept.