Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 559
Howard M. Lewis Ship Avatar asked Mar 22 '12 17:03

Howard M. Lewis Ship


People also ask

How do you handle HTTP request using servlet?

To handle HTTP requests in a servlet, extend the HttpServlet class and override the servlet methods that handle the HTTP requests that your servlet supports. This lesson illustrates the handling of GET and POST requests. The methods that handle these requests are doGet and doPost .

What is the default HTTP method in the servlet?

The default service() method in an HTTP servlet routes the request to another method based on the HTTP transfer method (POST, GET, and so on). For example, HTTP POST requests are routed to the doPost() method, HTTP GET requests are routed to the doGet() method, and so on.

What is HTTP servlet request and HTTP servlet response?

The HttpServletRequest object can be used to retrieve incoming HTTP request headers and form data. The HttpServletResponse object can be used to set the HTTP response headers (e.g., content-type) and the response message body. In Line 13, we set the "MIME" type of the response message to " text/html ".

Which method of HTTP servlet is used for getting the information from server?

GET method type and doGet() method The doGet() method in servlets is used to process the HTTP GET requests. So, basically, the HTTP GET method should be used to get the data from the server to the browser. Although in some requests, the GET method is used to send data from the browser to the server also.


1 Answers

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);
  }
}
like image 129
AMIC MING Avatar answered Sep 16 '22 12:09

AMIC MING