JSP 编程实现用户的登录
时间: 2024-05-08 14:15:22 浏览: 11
以下是基本的 JSP 编程实现用户登录的代码:
1. 编写登录页面 login.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h1>用户登录</h1>
<form method="post" action="loginCheck.jsp">
<p>
<label for="username">用户名:</label>
<input type="text" name="username" id="username" required>
</p>
<p>
<label for="password">密码:</label>
<input type="password" name="password" id="password" required>
</p>
<p>
<input type="submit" value="登录">
</p>
</form>
</body>
</html>
```
2. 编写登录验证页面 loginCheck.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录验证</title>
</head>
<body>
<%
// 获取用户输入的用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 建立数据库连接
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String pwd = "password";
conn = DriverManager.getConnection(url, user, pwd);
// 查询数据库中是否存在该用户
String sql = "select * from user where username=? and password=?";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.next()) {
// 用户名和密码匹配,登录成功
session.setAttribute("username", username);
response.sendRedirect("index.jsp");
} else {
// 用户名或密码错误,登录失败
out.println("用户名或密码错误,请重新登录!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
%>
</body>
</html>
```
3. 编写登录成功后跳转的页面 index.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>欢迎您,<%= session.getAttribute("username") %></h1>
<p><a href="logout.jsp">退出登录</a></p>
</body>
</html>
```
4. 编写退出登录页面 logout.jsp
```
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>退出登录</title>
</head>
<body>
<%
// 销毁 session
session.invalidate();
// 跳转到登录页面
response.sendRedirect("login.jsp");
%>
</body>
</html>
```
以上代码实现了一个简单的用户登录功能,包含了登录页面、登录验证页面、登录成功后跳转的页面和退出登录页面。其中,登录验证页面中使用了 JDBC 连接数据库,查询用户是否存在,如果存在,则将用户名存入 session 中,然后跳转到登录成功后的页面。退出登录页面中销毁了 session,然后跳转到登录页面。
相关推荐
![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)