在学习Servlet的过程中,我们大多时候编码都是直接继承HttpServlet这个类,并且重写doGet ,doPost,但是为什么要重写这两个方法?
从源码来看:
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);
}
}
从源码来看:获取客户端向服务器端传送数据所依据的协议名称的String类型(http1.1,http1.0)无论你是http1.1还是http1.0都会报http.method_get_not_supported这个错误,要避免错误,必须要重写覆盖源码中的doget ,dopost方法。