Programmatically enable GZIP Tomcat 7 (embedded)

依然范特西╮ 提交于 2019-12-24 07:04:37

问题


I am using Tomcat7 (embedded)

Something like this...

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

I have enabled the *.pdf mapping to jsp servlet (some issue I had with IE) is there a way to enable GZIP with this config (I have no web.xml, but if needed I could add to make it work) So far I have only found that I need to add this to my web.xml (which I don't have!)

<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:


I found that you can setup the properties like this:

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

I guess that this also applies for other connector property that you need to set up.



来源:https://stackoverflow.com/questions/15304921/programmatically-enable-gzip-tomcat-7-embedded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!