2.映射错误 错误示范:
赞
踩
**
** 解决方法: 网上很多说提交方式导致出现null的,但我试了一下,与提交方式无关,因为你无论用get还是post,都会用this调用此类中的另一个提交方式。 导致null出现一般两种情况
1.你所写的标签中没有name这个属性值 <input type="text" placeholder="用户名" name="username"> 2.映射错误 错误示范:<form action="/ChineseServlet" method="post"> 正确格式: <form action="ChineseServlet" method="post">
```java package web.servlet.Requwst; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.util.Enumeration; @WebServlet(name = "RequestDemo04", value = "/RequestDemo04") public class RequestDemo04 extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 2.获取指定参数的值 String username = request.getParameter("username"); System.out.println(username); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="ChineseServlet" method="post"> <input type="text" placeholder="用户名" name="username"><br> <input type="text" placeholder="密码" name="password"> <input type="submit" value="提交"> </form> </body> </html>