Java web servlet 关于启用页面压缩的相关配置
借助类,相关依赖:
<!-- https://mvnrepository.com/artifact/net.sourceforge.pjl-comp-filter/pjl-comp-filter --> <dependency> <groupId>net.sourceforge.pjl-comp-filter</groupId> <artifactId>pjl-comp-filter</artifactId> <version>1.7</version> </dependency>
在web.xml添加filter:
<filter> <filter-name>CompressingFilter</filter-name> <filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class> </filter> <filter-mapping> <filter-name>CompressingFilter</filter-name> <url-pattern>/*</url-pattern><!--对所有页面压缩--> </filter-mapping>
更详细的例子:
<filter> <filter-name>CompressingFilter</filter-name> <filter-class>com.planetj.servlet.filter.compression.CompressingFilter</filter-class> <init-param> <param-name>compressionThreshold</param-name><!-- 资源大小超过100byte的就被压缩 --> <param-value>100</param-value> </init-param> <init-param> <param-name>includeContentTypes</param-name><!-- 启动GZIP压缩的MIME类型 --> <param-value>text/html,text/xml,text/javascript,text/css,text/plain</param-value> </init-param> </filter> <filter-mapping> <filter-name>CompressingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>