servlet中避免405错误的产生
父类Parent(相当于HttpServlet):service方法,用于处理任务分发,doGet、doPost方法用于报错
关注的是子类Son(servlet)
目的:杜绝错误的产生
方式:
第一种:重写父类的service方法,必须去掉super.service(req, resp);
第二种:重写父类的doGet(去掉super.doGet();)、doPost(去掉super.doPost();)方法,调用父类的service方法
例如:
package com.bjsxt.second.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SecondServlet extends HttpServlet {
/*@Override
//第一种避免405错误的方法,重写servcie方法,去掉super.service(req, resp);
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//super.service(req, resp);
System.out.println("SecondServlet.service()");
}*/
@Override
//第二种避免405错误的方法,重写doGet方法,去掉super.doGet(req, resp);
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doGet(req, resp);
System.out.println("SecondServlet.doGet()");
}
@Override
//第二种避免405错误的方法,重写doPost方法,去掉super.doPost(req, resp);
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
//super.doPost(req, resp);
System.out.println("SecondServlet.doPost()");
}
}