關於"HTTP method GET is not supported by this URL"的錯誤
阿新 • • 發佈:2019-01-10
寫好一個Servlet後訪問時丟擲"HTTP method GET is not supported by this URL"的錯誤,先是自己找了一下原因,後又在網路查詢相關的原因後找到解決方案。
問題的原因是用Eclipse生成Servlet時,會在doGet和doPost自動新增預設呼叫父類的構造方法,如下紅色標識程式碼:
super.doPost();
super.doGet();
/** * @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錯誤,表示對於指定資源的請求方法不被允許。刪除以上程式碼當中呼叫父類的方法後問題迎刃而解。
文章出處:http://www.diybl.com/course/3_program/java/javajs/2008923/144667.html