javaweb做一个用户登录页面,有用户ID,用户密码输入框,登录和重置按钮,字体红色,所有字体均是红色 2 用户ID输入框获取焦点的时候边框变成浅蓝也失去焦点的时候边框变成原来的灰色或者黑色;密码输入框效果也是这样 3点击登录按钮完成form表单验证: 用户ID不能为空,只能是字母 密码纯数字6位否则不跳转 校验通过跳转到showMess.html展示用户信息
时间: 2024-02-23 19:01:24 浏览: 92
好的,这里给出一份JavaWeb实现的代码示例,供参考:
1. HTML代码(login.html)
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
<style>
input {
font-size: 16px;
color: red;
border: 1px solid gray;
padding: 5px;
}
input:focus {
border-color: lightblue;
}
.error {
color: red;
font-size: 14px;
}
</style>
</head>
<body>
<form action="login" method="post">
<label>用户ID:</label>
<input type="text" name="userId" id="userId" required pattern="[a-zA-Z]+" />
<br /><br />
<label>密码:</label>
<input type="password" name="password" id="password" required pattern="\d{6}" />
<br /><br />
<input type="submit" value="登录" />
<input type="reset" value="重置" />
</form>
<div id="errorDiv"></div>
<script>
var userId = document.getElementById("userId");
var password = document.getElementById("password");
var errorDiv = document.getElementById("errorDiv");
userId.addEventListener("blur", function() {
this.style.borderColor = "gray";
});
userId.addEventListener("focus", function() {
this.style.borderColor = "lightblue";
});
password.addEventListener("blur", function() {
this.style.borderColor = "gray";
});
password.addEventListener("focus", function() {
this.style.borderColor = "lightblue";
});
function validateForm() {
var userIdValue = userId.value.trim();
var passwordValue = password.value.trim();
if (userIdValue === "") {
errorDiv.innerHTML = "<p class='error'>用户ID不能为空!</p>";
return false;
} else if (!/^[a-zA-Z]+$/.test(userIdValue)) {
errorDiv.innerHTML = "<p class='error'>用户ID只能是字母!</p>";
return false;
} else if (passwordValue === "") {
errorDiv.innerHTML = "<p class='error'>密码不能为空!</p>";
return false;
} else if (!/^\d{6}$/.test(passwordValue)) {
errorDiv.innerHTML = "<p class='error'>密码必须是6位纯数字!</p>";
return false;
} else {
return true;
}
}
var form = document.querySelector("form");
form.addEventListener("submit", function(event) {
if (!validateForm()) {
event.preventDefault();
}
});
</script>
</body>
</html>
```
2. Java代码(LoginServlet.java)
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String userId = request.getParameter("userId");
String password = request.getParameter("password");
if (userId.equals("admin") && password.equals("123456")) {
response.sendRedirect("showMess.html");
} else {
request.setAttribute("error", "用户名或密码错误!");
request.getRequestDispatcher("login.html").forward(request, response);
}
}
}
```
3. HTML代码(showMess.html)
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户信息</title>
</head>
<body>
<h1>欢迎登录!</h1>
<p>您的用户ID是:admin</p>
<p>您的密码是:123456</p>
</body>
</html>
```
这个示例代码中,使用了HTML、CSS和JavaScript来实现页面效果,使用了Java来实现表单验证和页面跳转。在表单验证中,使用了HTML5的required和pattern属性来实现必填和正则验证,同时使用JavaScript来实现输入框的边框变化和错误提示。在Java代码中,使用了Servlet来接收表单数据并进行验证,如果验证通过则跳转到showMess.html页面,否则返回登录页面并显示错误信息。
阅读全文