新写一个jsp项目之一:登录模块

参考:https://blog.csdn.net/qq_41301333/article/details/131202057

一、纯JSP方式实现用户登录功能
(一)实现思路
登录页面login.jsp,输入用户名和密码后,跳转到登录处理页面doLogin.jsp进行业务逻辑处理,登录成功,跳转到登录成功页面success.jsp,否则跳转到登录失败页面failure.jsp。
(二)实现步骤

 

<%--
  Created by IntelliJ IDEA.
  User: liu
  Date: 2024/3/28
  Time: 20:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>
        .a{
            border-style: solid ;
            border-color: purple;
            width: 30%;
            height: 40%;
            margin: auto;
            /*position: absolute;*/
            /*left: 0;*/
            /*top: 0;*/
            /*bottom: 0;*/
            /*right: 0;*/
            background-color: lightgrey;
        }
        .b{
            text-align: center ;
            background-repeat: no-repeat ;
            background-position:center center;
            background-size:cover;
            background-attachment: fixed;
        }
    </style>
</head>
<body background="./image/beijing.jpg" class='b'>
<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>
View Code
<%--
  Created by IntelliJ IDEA.
  User: liu
  Date: 2024/3/28
  Time: 20:07
  To change this template use File | Settings | File Templates.
--%>



<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户登录</title>
</head>
<body>
<%
    // 获取登录表单数据
    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);
    }
%>
</body>
</html>
View Code
<%--
  Created by IntelliJ IDEA.
  User: liu
  Date: 2024/3/28
  Time: 20:07
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>
login.jsp
<%--
  Created by IntelliJ IDEA.
  User: liu
  Date: 2024/3/28
  Time: 20:08
  To change this template use File | Settings | File Templates.
--%>
<%@ 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>
View Code
<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: liu
  Date: 2024/3/28
  Time: 20:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录失败</title>
</head>
<body>
<h3 style="text-align: center">抱歉,<%=request.getParameter("username")%>,登录失败!</h3>
<%
    // 打印当前日期和时间
    System.out.println(request.getParameter("username"));
%>
</body>
</html>
View Code

 

posted @ 2024-03-28 22:15  JarvisLau  阅读(53)  评论(0编辑  收藏  举报