Servlet体系结构
Servlet接口
Genericservlet--抽象类
Httpservlet--抽象类
Genericservlet:将Servlet接口中其他的方法做了默认空实现,只将``service()`方法作为抽象
定义Servlet类时,可以继承Genericservlet,实现service()
方法即可
@WebServlet("/Demo")
public class TestGenericServlet extends GenericServlet {
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("GenericServlet类只是抽象出了service()...");
}
}
1
2
3
4
5
6
7
2
3
4
5
6
7
Httpservlet:对Http协议的一种封装,简化操作
1.定义类继承Httpservlet
2.复写doGet/doPost方法
不管使用哪种方法,都要实现service()
方法
HttpServlet源码
Http七种请求方式的判断
【Demo目录结构】
TestHttpServlet
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: Mr.Q
* @Date: 2020-01-30 10:23
* @Description:
*/
@WebServlet("/HttpServletDemo")
public class TestHttpServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doGet()...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doPost()...");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
doGet()
请求方式
访问http://localhost:8080/HttpServletDemo
Login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Post——Commit</title>
</head>
<body>
<form action="/HttpServletDemo" method="post">
<input name="Username">
<br>
<input type="submit" value="commit">
</form>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
doPost()
请求方式
访问http://localhost:8080/Login.html
提交表单之后为doPost()
提交表单之后为doPost()
编辑 (opens new window)
上次更新: 2021/06/27, 10:49:09