(1)login.html:编写登录页面,用户输入用户名和密码。 (2)check.jsp:获取表单输入的用户名和密码,判断密码 和用户名是否正确(直接在程序中设定用户名和密码), 如果正确,将用户名写入session对象并跳转到index.jsp ,否则跳转到登录页面。 (3)indexjsp:登录成功以后在该页面显示用户名,若直 接登录该页面,则跳转到登录页面。 (4)单击indexjsp页面中的退出按跳转到logout.jsp页面代码
时间: 2024-02-18 07:01:46 浏览: 67
1. `login.html`:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<form action="check.jsp" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
2. `check.jsp`:
```jsp
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("admin") && password.equals("password")) {
session.setAttribute("username", username);
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("login.html");
}
%>
```
3. `index.jsp`:
```jsp
<%
String username = (String) session.getAttribute("username");
if(username == null) {
response.sendRedirect("login.html");
} else {
%>
<!DOCTYPE html>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<h2>Welcome <%= username %>!</h2>
<a href="logout.jsp">Logout</a>
</body>
</html>
<%
}
%>
```
4. `logout.jsp`:
```jsp
<%
session.invalidate();
response.sendRedirect("login.html");
%>
```
以上是一个基本的登录和退出功能的实现,仅供参考。需要注意的是,在实际应用中,密码应该是加密存储的,登录页面也应该有验证码等安全措施。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)