2013-03-08 30 views
1

我使用Tomcat7(嵌入式)编程方式启用Gzip的Tomcat 7(嵌入式)

事情是这样的......

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,但如果需要我可以添加使其工作) 到目前为止,我只发现我需要将此添加到我的网站.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”/> 

回答

3

我发现,你可以设置的属性是这样的:

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"); 

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