Servlet的体系结构
Servlet -- 接口
GenericServlet -- 抽象类
HttpServlet -- 抽象类
GenericServlet :将Servlet接口中的其它方法做了默认空实现,只将service()方法作为抽象方法
##将来定义servlet类时,可以继承GenericServlet,实现service()方法即可
package cm.it.jason.web.servlet;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
/**
* @Author: Jason
* @Date: 2019/10/14 17:40
* @Version 1.0
*/
@WebServlet("/demo02")
public class ServletDemo02 extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("service方法实现!");
}
}
HttpServlet :对http协议的一种封装,简化操作
1.定义类继承HttpServlet
2.复写doGet/doPost方法
package cm.it.jason.web.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @Author: Jason
* @Date: 2019/10/14 18:07
* @Version 1.0
*/
@WebServlet("/demo03")
public class ServletDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
System.out.println("doGet.....");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
System.out.println("doPost.....");
}
}
doPost用表单提交的方式访问运行:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/demo03" method="post">
<input name="username">
<input type="submit" value="提交">
</form>
</body>
</html>