You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jetty Gzip Handler(JBoss AS7)在web.xml中的配置方法

Replacing Deprecated GzipFilter with GzipHandler in Jetty (JEE7 App)

Great question! Let's start with a key clarification: GzipHandler isn't a Servlet Filter, so you can't configure it directly in web.xml like you did with GzipFilter. It's a Jetty-specific Handler component, which means we need to use Jetty's own configuration files instead of the standard Servlet web.xml.

Here's how to make the switch properly:

Step 1: Remove the Deprecated GzipFilter from web.xml

First, delete the entire GzipFilter configuration block from your web.xml—we won't need it anymore:

<!-- Remove these lines from web.xml -->
<filter>
  <filter-name>GzipFilter</filter-name>
  <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
  <init-param>
    <param-name>mimeTypes</param-name>
    <param-value>text/html,text/plain,text/xml,application/xhtml+xml,application/xml,text/css,application/javascript,image/svg+xml,application/json,application/xml; charset=UTF-8</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>GzipFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

Step 2: Configure GzipHandler via Jetty's Web App Configuration

Create a file named jetty-web.xml in your app's WEB-INF directory (this is Jetty's per-web-app configuration file). Add the following content to replicate your original GzipFilter behavior with GzipHandler:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  <!-- Wrap the web app context with GzipHandler -->
  <Set name="handler">
    <New class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
      <!-- Link to the original web app handler -->
      <Set name="handler">
        <Ref refid="ContextHandler"/>
      </Set>
      <!-- Define the same mime types to compress as your old filter -->
      <Set name="includedMimeTypes">
        <Array type="java.lang.String">
          <Item>text/html</Item>
          <Item>text/plain</Item>
          <Item>text/xml</Item>
          <Item>application/xhtml+xml</Item>
          <Item>application/xml</Item>
          <Item>text/css</Item>
          <Item>application/javascript</Item>
          <Item>image/svg+xml</Item>
          <Item>application/json</Item>
          <Item>application/xml; charset=UTF-8</Item>
        </Array>
      </Set>
      <!-- Optional: Adjust compression settings if needed (defaults shown) -->
      <Set name="minGzipSize">256</Set> <!-- Only compress files larger than 256 bytes -->
      <Set name="compressionLevel">6</Set> <!-- Balanced speed/compression -->
    </New>
  </Set>
</Configure>

Bonus: Embedded Jetty Configuration (If Applicable)

If you're running Jetty embedded (i.e., starting it via your own Java code), you can configure GzipHandler directly in code instead of using jetty-web.xml:

// Create your web app context
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath("/");
webAppContext.setWar("path/to/your/application.war");

// Initialize and configure GzipHandler
GzipHandler gzipHandler = new GzipHandler();
gzipHandler.setHandler(webAppContext);
// Match your original mime type list
gzipHandler.setIncludedMimeTypes(
    "text/html", "text/plain", "text/xml",
    "application/xhtml+xml", "application/xml", "text/css",
    "application/javascript", "image/svg+xml", "application/json",
    "application/xml; charset=UTF-8"
);

// Attach to the server
Server server = new Server(8080);
server.setHandler(gzipHandler);
server.start();

Once you've made these changes, restart your Jetty server—the deprecation warning should disappear, and your content compression will work just like it did with the old filter.

内容的提问来源于stack exchange,提问作者Makhz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:13:08