HTML/CSS/JS 实现简单登录页面并重定向 - 用户名密码验证
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
<style>
body {
background-color: #f7f7f7;
}
form {
margin: 100px auto;
width: 300px;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px #ccc;
}
input {
display: block;
margin: 10px auto;
padding: 5px;
width: 100%;
font-size: 16px;
border: 1px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
input[type="submit"] {
background-color: #007fff;
color: #fff;
cursor: pointer;
transition: background-color 0.3s ease;
}
input[type="submit"]:hover {
background-color: #0066cc;
}
.error {
color: red;
font-size: 14px;
margin-top: 10px;
text-align: center;
}
</style>
</head>
<body>
<form action="Check.jsp" method="post" onsubmit="return checkForm()">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" placeholder="请输入用户名">
<label for="password">密码:</label>
<input type="password" id="password" name="password" placeholder="请输入密码">
<input type="submit" value="登录">
</form>
<script>
function checkForm() {
var username = document.getElementById("username").value.trim();
var password = document.getElementById("password").value.trim();
if (username === '' || password === '') {
alert('用户名和密码不能为空!');
return false;
}
return true;
}
</script>
</body>
</html>
<p>/* 无需添加CSS代码 */</p>
<p><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if ("admin".equals(username) && "123".equals(password)) {
response.sendRedirect("Success.jsp");
} else {
response.sendRedirect("Error.jsp");
}
%></p>
<p><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%></p>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>成功</title>
<style>
body {
background-color: #f7f7f7;
}
.container {
margin: 100px auto;
width: 300px;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px #ccc;
text-align: center;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
color: #007fff;
}
</style>
</head>
<body>
<div class="container">
<h1>欢迎登录</h1>
</div>
</body>
</html>
<p><%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%></p>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>错误</title>
<style>
body {
background-color: #f7f7f7;
}
.container {
margin: 100px auto;
width: 300px;
padding: 20px;
background-color: #fff;
box-shadow: 0 0 10px #ccc;
text-align: center;
}
h1 {
font-size: 24px;
margin-bottom: 20px;
color: red;
}
p {
font-size: 16px;
margin-bottom: 20px;
}
.countdown {
font-size: 16px;
color: #007fff;
}
.redirect {
font-size: 16px;
margin-top: 20px;
}
</style>
<script>
var countdown = 5;
window.setInterval(function() {
countdown--;
if (countdown === 0) {
window.location.href = "Login.html";
} else {
document.getElementById("countdown").innerHTML = countdown;
}
}, 1000);
</script>
</head>
<body>
<div class="container">
<h1>用户名或密码错误</h1>
<p>该页面显示<span id="countdown" class="countdown">5</span>秒后自动回到用户登录页面。</p>
<p class="redirect">如果没有自动跳转,请点击<a href="Login.html">这里</a>.</p>
</div>
</body>
</html>
AI 优化版
原文地址:https://www.cveoy.top/t/topic/oiay 著作权归作者所有。请勿转载和采集!