servlet错误:HTTP Status 405 - HTTP method GET is not supported by this URL

今天简单的做了个例子在 response.sendRedirect("list"); 重定向时报 405错误;

原因 :当用servlet类的URL直接访问servlet时,发送的是HTTP请求,servlet默认调用doGet()方法,而源程序里没有重写父类的doGet()方法,父类的doGet()方法不被此种URL支持,所以出现HTTP method GET is not supported by this URL这种错误

解决:ListEmpServlet.java 类重写doge()方法

页面

    <form action="addEmp" method="get">
        <fieldset>
            <legend>新增人员</legend>
            姓名:<input name="name"/><br/>
            薪水:<input name="salary"/><br/>
            年龄:<input name="age"/><br/>
            <input type="submit" value="提 交"/>
            <input type="reset" value="重 置"/>
        </fieldset>
    </form>

web.xml

         <servlet>
          <servlet-name>addEmpServlet</servlet-name>
          <servlet-class>servlet.AddEmpServlet</servlet-class>
      </servlet>   
      <servlet-mapping>
          <servlet-name>addEmpServlet</servlet-name>
          <url-pattern>/addEmp</url-pattern>
      </servlet-mapping>   
      <servlet>
          <servlet-name>listEmpServlet</servlet-name>
          <servlet-class>servlet.ListEmpServlet</servlet-class>
      </servlet> 
      <servlet-mapping>
          <servlet-name>listEmpServlet</servlet-name>
          <url-pattern>/list</url-pattern>
      </servlet-mapping>

AddEmpServlet.java  

     public class AddEmpServlet extends HttpServlet{
        public void service(HttpServletRequest request,
            HttpServletResponse response) throws
            ServletException,IOException{
        response.sendRedirect("list");}

ListEmpServlet.java

public class ListEmpServlet extends HttpServlet {
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
        //this.doGet(request,response);
        }
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
      System.out.println("2");
       // this.service(request, response);
        this.Server(request, response);
        }
    public void Server(HttpServletRequest reques,
            HttpServletResponse response)throws
            ServletException,IOException{
        System.out.println("3");

  }

你可能感兴趣的:(servlet)