父類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()");
}
}