html
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2024/6/7
Time: 8:38
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/demo/login">前往登录</a>
</body>
</html>
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>aa</servlet-name>
<servlet-class>com.yanyu.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>aa</servlet-name>
<url-pattern>/demo/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>bb</servlet-name>
<servlet-class>com.yanyu.Demmo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>bb</servlet-name>
<url-pattern>/list</url-pattern>
</servlet-mapping>
</web-app>
java
package com.yanyu;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class Login extends HttpServlet {
// 重写 父类方法 ctrl o
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// super.doGet(req, resp); 调用 父类的方法 super == 父类 的对象
// 设置 响应的类型和 编码格式
/* 浏览器 --》 服务器 的 数据 request
服务器 --》 浏览器 的 数据 response
* request
* response
* */
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
// 获取响应对象
PrintWriter out = response.getWriter();// ctrl alt v
// out.println("<html lang=\"en\">");
out.println("hello3");
// 多行 编辑 alt 鼠标拖动
out.println(" <!DOCTYPE html>");
out.println("");
out.println("<head>");
out.println(" <meta charset='UTF-8'>");
out.println(" <meta http-equiv='X-UA-Compatible' content='IE=edge'>");
out.println(" <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
out.println(" <title>Document</title>");
out.println("</head>");
out.println("<body>");
out.println(" <h1>登录界面</h1>");
out.println(" <form action='/list'>");
// src = href = action 都是 路径 都是 url
out.println(" 账号<input type='text' name='username' >");
out.println(" <br>");
out.println(" 密码<input type='password' name='password' >");
out.println(" ");
out.println(" <br>");
out.println(" <input type='submit' value='登录'>");
out.println(" </form>");
out.println("</body>");
out.println("</html>");
}
}
java
package com.yanyu;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class Demmo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取 浏览器 的数据 request
// 设置 请求 编码
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");// CTRL alt v
String pass = request.getParameter("password");// CTRL alt v
// 通过 name 属性的 值 username 去 拿到 账号
System.out.println(username);/// 打印到服务器控制台 4232354
System.out.println(pass);/// 打印到服务器控制台 4232354
}
}