Servlet HTTP method GET is not supported HTTP 405

寫好一個Servlet後訪問時拋出"HTTP method GET is not supported by this URL"的錯誤,先是本身找了一下緣由,後又在網絡查找相關的緣由後找到解決方案。 java

問題的緣由是用Eclipse生成Servlet時,會在doGet和doPost自動添加默認調用父類的構造方法,以下紅色標識代碼: 網絡


/**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doGet(request, response);
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doPost(request, response);
    }
這個時候就會有個問題,若是直接調用父類的方法,就至關於父類HttpServlet的doGet或doPost方法覆蓋了你重寫的方法,而父類 HttpServlet的doGet或doPost方法的默認實現是返回狀態代碼爲405的HTTP錯誤,表示對於指定資源的請求方法不被容許。刪除以上 代碼當中調用父類的方法後問題迎刃而解。
相關文章
相關標籤/搜索