package day03;
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 RequestDemo02 extends HttpServlet {
private static final long serialVersionUID = -8885728678471757078L;
/**
* @author 半步疯子
*
* 这个service方法是servlet的核心的服务方法。我们的业务逻辑都是在这个方法开始被触发的。
* 这是一个入口:调用其它方法之前,都会调用service方法。
*
* 在新的testMethod中进行调用的时候发现,进行数据提交的时候,
* 不管是使用doGet提交的,还是调用doPost进行提交的;
* 最后都是只调用的service进行的,并没有调用doGet和doPost。
*
*
* 证明service才是最基本的入口。
*
* 但是当我们没有重写service的时候,为什么还是调用的是
* doPost和doGet呢?(源码剖析)
*
* service中通过request的不同的do方法,在内部调用不同的方法
* doGet、doPost、doPut... ...
*
* 因为进入之后,必须调用service方法,如果我们在重写的service方法
* 中没有调用其中的doGet和doPost方法的话,进入service之后就不会进
* 行其它的操作。
*
*
* 代码逻辑就在service这个方法中被调用到。
*
*
* 所以在开发中不建议覆盖service方法;
* 建议覆盖doXXX方法。
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("调用了service方法");
// 源码中的样式:
// 源码中用:GET代表doGet、POST代表doPost。
String method = request.getMethod();
System.out.println(method);
if("GET".equals(method)){
doGet(request, response);
} else if("POST".equals(method)) {
doPost(request, response);
} else {
System.out.println("do nothing!");
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("调用了doGet方法");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("调用了doPost方法");
}
}