10.2.3 Executing the applications that use the HTTP response compression filter
This section describes the precautions to be taken when developing applications that use the HTTP response compression filter.
- Organization of this subsection
(1) Order of invocation when the HTTP response compression filter is combined with other filters
The HTTP response compression filter must be invoked before the other filters specified in the HTTP response header. When you use the setHeader method, addHeader method, setIntHeader method, and addIntHeader method of javax.servlet.http.HttpServletResponse to use other filters that set Content-Length header and Content-Encoding header, deploy the other filters after the HTTP response compression filter.
(2) Precautions related to HTTP response buffer
When the HTTP response compression functionality is enabled, the buffer with the size specified in the compression threshold is installed before the HTTP response buffer. Data is written in the HTTP response buffer when the output data exceeds the compression threshold.
Unless the compressed data size exceeds the HTTP response buffer size, the HTTP response is not written in the Web client. If you are required to write the HTTP response in the Web client before the output data size exceeds the compression threshold, you must explicitly invoke the flush method of the stream for response output#. However, if the flush method or flushBuffer method of the javax.servlet.ServletResponse interface is invoked before the output data size exceeds the compression threshold, the output data is written in the Web client without being compressed.
- #
-
The stream for response output indicates the following objects:
-
javax.servlet.ServletOutputStream acquired by the getOutputStream method of the javax.servlet.ServletResponse interface
-
java.io.PrintWriter acquired by the getWriter method of the javax.servlet.ServletResponse interface
-
javax.servlet.jsp.JspWriter implicitly available in JSP
-
(3) Precautions related to the response header of the HTTP response
When the response body of the HTTP response is compressed with the HTTP response compression functionality, gzip is specified in the Content-Encoding header and Accept-Encoding is specified in the Vary header of this HTTP response. Nothing is specified in the Content-Length header.
Therefore, note the following points when you use the setContentLength method of the javax.servlet.ServletResponse interface and when you use API# for adding and changing the response header of the javax.servlet.http.HttpServletResponse interface:
-
When you use one of the following APIs to add the filter for setting the Content-Length header and Content-Encoding header, define in the DD (web.xml) that the API be executed after the filter for response compression:
-
setContentLength method of the ServletResponse class
-
API# for adding and changing the response header of the HttpServletResponse class
-
-
When the response body of the HTTP response is compressed, the Content-Length header of the HTTP response is not added even though the setContentLength method of the ServletResponse class and the API# for adding and changing the response header of the HttpServletResponse class are used. The HTTP response for which the Content-Length header is not added is sent in the chunk format to the client by the Web container.
-
When the response body of the HTTP response is compressed, a value is not set in the Content-Encoding header even though the API# for adding and changing the response header of the HttpServletResponse class is used. When the response body is compressed, gzip is specified in the Content-Encoding header by the Web Container.
- #
-
The API for adding and changing the response header indicates the following methods of the javax.servlet.http.HttpServletResponse interface:
-
setHeader method
-
addHeader method
-
setIntHeader method
-
addIntHeader method
-
(4) Precautions related to data output for HTTP response
Note the following points when ServletOutputStream or PrintWriter is acquired with the getOutputStream method or the getWriter method of the javax.servlet.ServletResponse interface and the HTTP response is output:
-
When you invoke the setContentType method of ServletResponse while the data is being written in the HTTP response buffer by using ServletOutputStream or PrintWriter, even if HTTP response with Media-Type specified for compression exists, the HTTP response is not compressed.
However, if an asterisk (*) is specified in the Media-Type to be compressed, the HTTP response is compressed.
-
To compress the JSP output by specifying Media-Type, either specify the contentType attribute of the Page directive or invoke the setContentType method of the ServletResponse class before the JSP buffer is exceeded.
(5) Precautions for compressing HTTP responses in applications
For HTTP responses compressed in applications, specify settings so that the HTTP response compression functionality is not enabled. If the HTTP response compression functionality is enabled for the HTTP responses compressed in applications, the operations might not function properly.