采用继承HttpServlet类运行Servlet程序时,有时会出现405报错,而采用实现Servlet的方法就不会,这是因为父类中实现了Servlet接口后,对内部方法进行了空实现,并新增了两个新的方法,子类在继承时需要重写方法,否则会抛出异常,解决方法如下:
重写父类中的**service()、doGet()、doPost()**方法,并分别删掉下列三条语句:
super.service(req,resp);
super.doGet(req, resp);
super.doPost(req, resp);
最终结果如下:
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//重写的代码
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//重写的代码
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//重写的代码
}
}