用IDEA实现Tomcat然后在进行一点改变
1:首先创建一个com.cai.ser(这里名字可以自己取)然后在里面创建一个Login
package com.cai.ser;
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 Login extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
String user = req.getParameter("user");
String pwd = req.getParameter("pwd");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
if (user.equals("张三") && pwd.equals("123456")) {//假登陆验证账号和密码并判断
resp.getWriter().write("登录成功!");
} else {
resp.getWriter().write("登录失败!");
}
}
}
2:更改web.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>login</servlet-name>
<servlet-class>com.cai.ser.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
3:在index.jsp中设置你自己喜欢的页面样式我这里就简单写了
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2023/2/14
Time: 18:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<a href="login.jsp">登录</a>
</body>
</html>
4:自己在web创建一个创建一个log.jsp文件并在里面写上一下内容
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2023/2/14
Time: 18:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录</title>
</head>
<body>
<form action="login" method="post">
账号:<input type="text" name="user" value=""><br>
密码:<input type="password" name="pwd" value=""><br>
<input type="submit" value="登录">
</form>
</body>
</html>
5:测试结果如果账号密码都正确就是成功如果不是就是失败