以编程方式启用GZIP Tomcat 7(嵌入式)

时间:2013-03-08 23:13:26

标签: tomcat tomcat7 embedded-tomcat-7

我正在使用Tomcat7(嵌入式)

像这样......

String APP_DIR = "ROOT";
Tomcat current = new Tomcat();
File file = new File(APP_DIR);
if (file.isDirectory() && file.canRead()) {
    ctx = current.addWebapp(null, "", file.getAbsolutePath());
    ctx.setSessionCookiePathUsesTrailingSlash(false);
}
current.start();
ctx.addServletMapping("*.pdf", "jsp", true);

我启用了* .pdf映射到jsp servlet(我在IE上遇到的一些问题) 有没有办法使用此配置启用GZIP(我没有web.xml,但如果需要我可以添加以使其工作) 到目前为止,我只发现我需要将它添加到我的web.xml(我没有!)

<Connector port=”8080″ maxHttpHeaderSize=”8192″
maxThreads=”150″ minSpareThreads=”25″ maxSpareThreads=”75″
enableLookups=”false” redirectPort=”8443″ acceptCount=”100″
connectionTimeout=”20000″ disableUploadTimeout=”true”
compression=”on”
compressionMinSize=”2048″
noCompressionUserAgents=”gozilla, traviata”
compressableMimeType=”text/html,text/xml”/>

1 个答案:

答案 0 :(得分:4)

我发现您可以设置如下属性:

Tomcat current = new Tomcat();
Connector c = this.current.getConnector();
c.setProperty("compression", "on");
c.setProperty("compressionMinSize", "1024");
c.setProperty("noCompressionUserAgents", "gozilla, traviata");
c.setProperty("compressableMimeType", "text/html,text/xml, text/css, application/json, application/javascript");

我想这也适用于您需要设置的其他连接器属性。