Howard M. Lewis Ship
Howard M. Lewis Ship

Reputation: 2337

Using Servlet API, how to determine if request was HTTP/1.0 or HTTP/1.1?

I'm fixing a bug that is only evidenced when the client is using HTTP/1.0 (and is secretly, Internet Explorer proxying behind a firewall).

Details are here: https://issues.apache.org/jira/browse/TAP5-1880

In any case, the right solution is to turn off a feature (GZip content compression) when the request is HTTP/1.0. However, after scouring the Servlet API documentation, and even the Jetty source, I can't find any place where this information is exposed.

So, is there a way to determine this? I'm using Servlet API 2.5.

Thanks in advance!

Upvotes: 8

Views: 4616

Answers (1)

AMIC MING
AMIC MING

Reputation: 6354

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

Here is the example, execute in your local 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);
  }
}

Upvotes: 12

Related Questions