用jsp写一个登录页面附带定时刷新吗
时间: 2024-03-08 16:48:24 浏览: 20
可以,以下是一个简单的示例:
```jsp
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<%@ page import="java.util.*" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="login.jsp" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
<%
// 处理登录逻辑
String username = request.getParameter("username");
String password = request.getParameter("password");
if ("admin".equals(username) && "123456".equals(password)) {
session.setAttribute("username", username);
response.sendRedirect("home.jsp");
} else {
out.println("<p style='color:red'>Invalid username or password</p>");
}
%>
<%
// 定时刷新页面
response.setIntHeader("Refresh", 30); // 30秒自动刷新
%>
</body>
</html>
```
在这个示例中,我们使用 JSP 实现了一个简单的登录页面,并在页面中加入了定时刷新的功能。当用户提交登录表单时,后台会根据输入的用户名和密码进行验证,并在验证通过后将用户名存入 session 并跳转到主页。如果验证失败,则会在页面上显示错误信息。同时,我们通过设置 `response.setIntHeader("Refresh", 30)` 实现了页面的自动刷新,每隔 30 秒重新加载页面。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)