• 编写一个Servlet类,直接继承HttpServlet
    • 重写doGet方法或者重写doPost方法,到底写谁?Javaweb程序员说了算 ```java package servlet;

    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;

    /**

    • @Author: 小雷学长
    • @Date: 2022/3/20 - 15:24
    • @Version: 1.8 */ public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp)

      1. throws ServletException, IOException {
      2. resp.setContentType("text/html");
      3. PrintWriter out = resp.getWriter();
      4. out.print("<h1>登录成功……</h1>");

      } }

    1. - [x] Servlet类配置到web.xml文件当中
    2. ```xml
    3. <?xml version="1.0" encoding="UTF-8"?>
    4. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    6. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    7. version="4.0">
    8. <servlet>
    9. <servlet-name>login</servlet-name>
    10. <servlet-class>servlet.LoginServlet</servlet-class>
    11. </servlet>
    12. <servlet-mapping>
    13. <servlet-name>login</servlet-name>
    14. <url-pattern>/login</url-pattern>
    15. </servlet-mapping>
    16. </web-app>
    • [x] 准备前端的页面(form表单),form表单中指定请求路径路径即可,

      1. <!DOCTYPE html>
      2. <html lang="en">
      3. <head>
      4. <meta charset="UTF-8">
      5. <title>Login page</title>
      6. </head>
      7. <body>
      8. <form action="/Servlet00/login" method="post">
      9. <input type="submit" value="login">
      10. </form>
      11. </body>
      12. </html>

      image.png
      image.png