JavaWeb课程系列
3.5 HttpServlet抽象类
1)HttpServlet继承了GenericServlet,并实现了service方法。在service方法中,将ServletRequest和ServletResponse转换为了HttpServletRequest和HttpServletResponse,用来专门处理我们的Http请求。
2) 方法在完成对请求和响应的强转之后调用了 方法,在被调用的方法中 对请求类型进行了判断,各种请求调用自己相应的doXXX方法。而我们常用的就是doGet()和doPost()方法。
3)在我们以后的使用中,都使用继承HttpServlet的方式,重写doGet和doPost方法即可。在浏览器发送请求的时候,如果是get请求,将会调用doGet()方法,如果是post请求,将会调用doPost()方法
4)继承HttpServlet的新的Servlet写法如下(web.xml配置与之前相同):
public class FirstServlet extends HttpServlet { private static final long serialVersionUID = 1L; public FirstServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doGet()......"); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("doPost()......"); } }
|