使用Servlet API,如何确定请求是HTTP / 1.0还是HTTP / 1.1?

时间:2012-03-22 17:27:28

标签: java http servlets

我正在修复一个错误,只有在客户端使用HTTP / 1.0时(并且秘密地​​,Internet Explorer代理在防火墙后面)才能证明这个错误。详细信息如下:https://issues.apache.org/jira/browse/TAP5-1880

在任何情况下,正确的解决方案是在请求为HTTP / 1.0时关闭功能(GZip内容压缩)。但是,在搜索了Servlet API文档,甚至是Jetty源代码之后,我找不到任何暴露此信息的地方。

那么,有没有办法确定这个?我正在使用Servlet API 2.5。

提前致谢!

1 个答案:

答案 0 :(得分:8)

request.getProtocol() will return "HTTP/1.0" or "HTTP/1.1"

这是一个例子,在你的本地tomcat中执行

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class ShowRequestHeaders extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Servlet Example: Showing Request Headers";
    out.println(ServletUtilities.headWithTitle(title) +
                "<BODY BGCOLOR=\"#FDF5E6\">\n" +
                "<H1 ALIGN=CENTER>" + title + "</H1>\n" +
                "<B>Request Method: </B>" +
                request.getMethod() + "<BR>\n" +
                "<B>Request URI: </B>" +
                request.getRequestURI() + "<BR>\n" +
                "<B>Request Protocol: </B>" +
                request.getProtocol() + "<BR><BR>\n" +
                "<TABLE BORDER=1 ALIGN=CENTER>\n" +
                "<TR BGCOLOR=\"#FFAD00\">\n" +
                "<TH>Header Name<TH>Header Value");
    Enumeration headerNames = request.getHeaderNames();
    while(headerNames.hasMoreElements()) {
      String headerName = (String)headerNames.nextElement();
      out.println("<TR><TD>" + headerName);
      out.println("    <TD>" + request.getHeader(headerName));
    }
    out.println("</TABLE>\n</BODY></HTML>");
  }

  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}