Sending Compressed Pages:
EncodedPage.java
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();
}
45
Servlets
www.corewebprogramming.com
EncodedPage.java (Continued)
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("