1. Pure JSP method to implement user login function
(1) Implementation ideas
- In the login page login.jsp, after entering the user name and password, it jumps to the login processing page doLogin.jsp for business logic processing. If the login is successful, it jumps to the login success page success.jsp. Otherwise, it jumps to the login failure page failure.jsp.
(2) Implementation steps
1. Create a Web project
- Start by creatingJava Enterpriseproject, addWeb ApplicationFunction
-
Set the project name to LoginDemo and set the save location
-
Click the [Finish] button
-
Modify the Artifact name in the project structure window - LoginDemo01 and delete the suffix.
-
Edit the server configuration and redeploy the project
-
First, the [Server] tab
-
Then switch to the [Deployment] tab
2. Create a login page
- Create login page -login.jsp
- code show as below
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form action="doLogin.jsp" method="post">
<table border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">账号</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
3. Create a login processing page
- Create login processing page -doLogin.jsp
- code show as below
<%
// 获取登录表单数据
String username = request.getParameter("username");
String password = request.getParameter("password");
// 判断登录是否成功
if (username.equals("无心剑") && password.equals("903213")) {
// 跳转到登录成功页面,传递用户名
response.sendRedirect("success.jsp?username=" + username);
} else {
// 跳转到登录失败页面,传递用户名
response.sendRedirect("failure.jsp?username=" + username);
}
%>
4. Create a login success page
- Login success page -success.jsp
- code show as below
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<h3 style="text-align: center">恭喜,<%=request.getParameter("username")%>,登录成功!</h3>
</body>
</html>
5. Create a login failure page
- Create login failure page -failure.jsp
- code show as below
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登录成功</title>
</head>
<body>
<h3 style="text-align: center">恭喜,<%=request.getParameter("username")%>,登录成功!</h3>
</body>
</html>
6. Edit project homepage
- Project Home Page -index.jsp
- code show as below
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<h3 style="text-align: center">用户登录</h3>
<form action="doLogin.jsp" method="post">
<table border="1" cellpadding="10" style="margin: 0px auto">
<tr>
<td align="center">账号</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td align="center">密码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
</td>
</tr>
</table>
</form>
</body>
</html>
(3) Test results
- Start the server and display the home page
- Click the [Jump to login page] hyperlink
- Enter the correct username and password (Wuxinjian: 903213)
- Click the [Login] button to jump to the login success page
- Return to the login page and enter the wrong username or password
- Screen recording operation