HTTP method GET is not supported by this URL

 

博客分类:
Java代码  收藏代码
  1. public class Servlet2 extends HttpServlet {  
  2.   
  3.   @Override  
  4.   protected void doGet(HttpServletRequest req,   
  5.       HttpServletResponse resp )throws ServletException, IOException {  
  6.     super.doGet(req, resp);  
  7.   }  
  8.       
  9.   @Override  
  10.   protected void doPost(HttpServletRequest req,   
  11.        HttpServletResponse resp)throws ServletException, IOException {  
  12.     //设置响应类型  
  13.     resp.setContentType("text/html;charset=utf-8") ;  
  14.     //从响应实例中获取打印流  
  15.     PrintWriter out = resp.getWriter() ;  
  16.     out.println("<html>") ;  
  17.     out.println("<head><title>servlet2</title></head>") ;  
  18.     out.println("<body>") ;  
  19.     out.println("从Servlet2中获取请求参数name的值:") ;  
  20.     out.println(req.getParameter("name")) ;  
  21.     out.println("</body>") ;  
  22.     out.println("</html>") ;  
  23.   }  
  24. }  

此时我们访问:http://localhost:8000/../servlet2?name=test 
就会出现: 
     HTTP Status 405 - HTTP method GET is not supported by this URL 
出现错误的原因 
  1,继承HttpServlet的Servlet没有覆写对应请求和响应的处理方法即:doGet或 
      doPost等方法;默认调用了父类的doGet或doPost等方法; 
  2, 父类HttpServlet的doGet()或doPost()方法覆盖了你重写的doGet或doPost等 
      方法; 
      只要出现以上的情况之一,父类HttpServlet的doGet或doPost等方法的默认实现是 
      返回状态代码为405的HTTP错误表示:对于指定资源的请求方法不被允许。 
解决方法 
  1,子类覆写父类的doGet或doPost等方法; 
  2,在你的Servlert中覆写doGet或doPost等方法来处理请求和响应,不要调用父类 
     HttpServlet的doGet() 和 doPost()方法,即: 
     将doGet()方法中的 super.doGet(req, resp); 
             改为:this.doPost(req , resp) ; 可以解决问题。
原文地址:https://www.cnblogs.com/appzhang/p/2707758.html