Java Web pure JSP way to realize user login function

1. Realize user login function in pure JSP mode

(1) Implementation ideas

  • The login page login.jsp, after entering the user name and password, jumps to the login processing page doLogin.jsp for business logic processing, if the login is successful, jumps to the login success page success.jsp, otherwise jumps to the login failure page failure.jsp.

(2) Implementation steps

1. Create a Web project

  • start by creatingJava Enterpriseitem, addWeb ApplicationFunction
    insert image description here

insert image description here

  • Set the project name to LoginDemo, and set the save location
    insert image description here

  • Click the [Finish] button
    insert image description here

  • In the project structure window, modify the Artifact name - LoginDemo01, and delete the suffix
    insert image description here
    insert image description here

  • Edit the server configuration, redeploy the project
    insert image description here

  • First the [Server] tab
    insert image description here

  • Then switch to the [Deployment] tab
    insert image description here

2. Create a login page

  • Create a login page −login.jsp
    insert image description here
  • 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
    insert image description here
  • 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 successful login page

  • Login success page -success.jsp
    insert image description here
  • 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
    insert image description here
  • 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 the project home page

  • Project Home -index.jsp
    insert image description here
  • 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
    insert image description here
  • Click the [Jump to login page] hyperlink
    insert image description here
  • Enter the correct username and password (Wuxinjian: 903213)
    insert image description here
  • Click the [Login] button to jump to the login success page
    insert image description here
  • Go back to the login page, enter wrong username or password
    insert image description here
  • Recording operation
    insert image description here

Guess you like

Origin blog.csdn.net/qq_64505257/article/details/130759522