In the process of learning Servlet, most of our coding is to directly inherit the HttpServlet class, and rewrite doGet, doPost, but why rewrite these two methods?
From the source code:
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
//该方法用于获取客户端向服务器端传送数据所依据的协议名称。
String protocol = req.getProtocol();
String msg = lStrings.getString("http.method_get_not_supported");
if (protocol.endsWith("1.1")) {
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, msg);
} else {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, msg);
}
}
From the source code point of view: Get the String type (http1.1, http1.0) of the protocol name based on which the client transmits data to the server. Whether you are http1.1 or http1.0, the error http.method_get_not_supported will be reported. Avoid it. Error, you must rewrite the doget and dopost methods in the source code.