1、方法
import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.PrintWriter; import java.net.HttpCookie;
/**
- @Author: 小雷学长
- @Date: 2022/3/20 - 17:22
@Version: 1.8 */ public class RequestTestServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置请求体的字符集
//以免Tomcat9级以下版本出现乱码
request.setCharacterEncoding("UTF-8");
//这是Post请求
String username = request.getParameter("username");
//输出这个用户提交的用户名
System.out.println(username);
}
}
```xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>RequestTestServlet</servlet-name>
<servlet-class>servlet.RequestTestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RequestTestServlet</servlet-name>
<url-pattern>/testrequest</url-pattern>
</servlet-mapping>
</web-app>