登录页面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h3>登录页面</h3> <form action="/day3/login" method="POST"> 账号: <input type="text" name="username"/><br/> 密码: <input type="text" name="password"/><br/> <input type="submit" value="朕要登录"/> </form> </body> </html>
解决了中文乱码问题
package per.mjn._06_encoding; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // 接收和处理登陆的信息 @WebServlet("/login") public class LoginServlet extends HttpServlet{ private static final long serialVersionUID = 1L; protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // System.out.println("LoginServlet.service()"); // 对于POST请求, 可以设置请求的编码 req.setCharacterEncoding("UTF-8"); String username = req.getParameter("username"); // // 1. 按照ISO-8859-1把乱码恢复成二进制形式 // byte[] data = username.getBytes("ISO-8859-1"); // // 2. 再把二进制形式的数据使用UTF-8重新编码 // username = new String(data, "UTF-8"); String password = req.getParameter("password"); System.out.println(username + ", " + password); } }