Servlet->Servlet体系根接口
GenericServlet->Servlet抽象实现类
HttpServlet->对HTTP协议封装的Servlet实现类
B/S架构的web项目都是针对HTTP协议,所以自定义的Servlet会继承HttpServlet
get与post请求方式不同,需要分别处理,它请求方式的判断是通过不同方式进行逻辑判断
HttpServlet使用:①继承HttpServlet②重写doGet和doPost方法
原理:根据不同请求方式调用不同方法
package test;
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;
@WebServlet("/demo2")
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("get");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("post");
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/demo2" method="post">
<input name="username"><input type="submit">
</form>
</body>
</html>
网友评论