chapter 5 客户请求的处理:http请求报头
5.1 请求报头的读取
在使用request.getHeader返回结果之前,一定要确保它不是null。
5.2 制作所有请求报头的表格
5.3 了解Http1.1请求报头
1.Accept 这个报头指定浏览器或其他客户程序能够处理的MIME类型。
request.getHeader("Accept")
2.Accept-Charset
浏览器可以使用的字符集
3.Accept-Encoding
客户端能够处理的编码类型
4.Accept-Language
标准语言代码的一种,比如en,en-us,da等
5.Authorization
6.connection
客户能否处理持续性的连接。
7.Content-Length
这个报头只适用于POST请求,用来给定POST数据的大小,以字节为单位。
8.Cookie
不要直接使用这个报头,而要使用request.getCookies
9.Host
10.If-Modified-Since
仅当页面在指定的日期之后发生更改的情况下,客户程序才希望获取该页面。
11.If-Unmodified-Since
12. Referer
一用web页面的URL
13.User-Agent
这个报头标识生成请求的浏览器或其他客户程序,根据这个报头,可以针对不同类型的浏览器返回不同的内容。
5.4 发送压缩web页面
gzip文本压缩方案可以极大地减少HTML页面的大小。
Implementing compression is straightforward since support for the gzip format is
built in to the Java programming language by classes in java.util.zip. The servlet
first checks the Accept-Encoding header to see if it contains an entry for gzip. If so,
it uses a PrintWriter wrapped around a GZIPOutputStream and specifies gzip
as the value of the Content-Encoding response header. If gzip is not supported, the
servlet uses the normal PrintWriter and omits the Content-Encoding header.
To make it easy to compare regular and compressed performance with the same
browser, we also added a feature whereby we can suppress compression by including
?disableGzip at the end of the URL
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/** Servlet with <B>long</B> output. Used to test
* the effect of the gzip compression.
*/
public class LongServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
// Change the definition of "out" depending on whether
// or not gzip is supported.
PrintWriter out;
if (GzipUtilities.isGzipSupported(request) &&
!GzipUtilities.isGzipDisabled(request)) {
out = GzipUtilities.getGzipWriter(response);
response.setHeader("Content-Encoding", "gzip");
} else {
out = response.getWriter();
}
// Once "out" has been assigned appropriately, the
// rest of the page has no dependencies on the type
// of writer being used.
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
String title = "Long Page";
out.println
(docType +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>\n");
String line = "Blah, blah, blah, blah, blah. " +
"Yadda, yadda, yadda, yadda.";
for(int i=0; i<10000; i++) {
out.println(line);
}
out.println("</BODY></HTML>");
out.close(); // Needed for gzip; optional otherwise.
}
}
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.zip.*;
/** Three small static utilities to assist with gzip encoding.
* <UL>
* <LI>isGzipSupported: does the browser support gzip?
* <LI>isGzipDisabled: has the user passed in a flag
* saying that gzip encoding should be disabled for
* this request? (Useful so that you can measure
* results with and without gzip on the same browser).
* <LI>getGzipWriter: return a gzipping PrintWriter.
* </UL>
*/
public class GzipUtilities {
/** Does the client support gzip? */
public static boolean isGzipSupported
(HttpServletRequest request) {
String encodings = request.getHeader("Accept-Encoding");
return((encodings != null) &&
(encodings.indexOf("gzip") != -1));
}
/** Has user disabled gzip (e.g., for benchmarking)? */
public static boolean isGzipDisabled
(HttpServletRequest request) {
String flag = request.getParameter("disableGzip");
return((flag != null) && (!flag.equalsIgnoreCase("false")));
}
/** Return gzipping PrintWriter for response. */
public static PrintWriter getGzipWriter
(HttpServletResponse response) throws IOException {
return(new PrintWriter
(new GZIPOutputStream
(response.getOutputStream())));
}
}