Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
106
Chapter 4 Handling the Client Request: HTTP Request Headers
Listing 4.2 EncodedPage.java
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.zip.*;
/** Example showing benefits of gzipping pages to browsers
* that can handle gzip.
*/
public class EncodedPage extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String encodings = request.getHeader("Accept Encoding");
String encodeFlag = request.getParameter("encoding");
PrintWriter out;
String title;
if ((encodings != null) &&
(encodings.indexOf("gzip") != 1) &&
!"none".equals(encodeFlag)) {
title = "Page Encoded with GZip";
OutputStream out1 = response.getOutputStream();
out = new PrintWriter(new GZIPOutputStream(out1), false);
response.setHeader("Content Encoding", "gzip");
} else {
title = "Unencoded Page";
out = response.getWriter();
}
out.println(ServletUtilities.headWithTitle(title) +
"\n" +
"" + title + "
\n");
String line = "Blah, blah, blah, blah, blah. " +
"Yadda, yadda, yadda, yadda.";
for(int i=0; i<10000; i++) {
out.println(line);
}
out.println("