设计一个简单登录页面login.jsp,输入用户名、密码,要求与student数据库中用户名和密码进行比较,如果一致,使用重定向跳转到success.jsp页面,提示“欢迎您,XXX”,否则,返回登录页面,并在登录页面中显示“用户名或密码出错,请重新输入”。
时间: 2024-05-15 22:19:40 浏览: 99
jsp实例:一个简单的用户登陆系统.pdf
```
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String message = "";
if (username != null && password != null) {
try {
// 连接数据库
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/student";
String user = "root";
String pwd = "123456";
Connection conn = DriverManager.getConnection(url, user, pwd);
// 查询数据库
String sql = "SELECT * FROM users WHERE username=? AND password=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
// 判断查询结果
if (rs.next()) {
// 登录成功,重定向到success.jsp页面
message = "欢迎您," + rs.getString("name");
session.setAttribute("username", username);
response.sendRedirect("success.jsp");
} else {
// 登录失败,显示错误信息
message = "用户名或密码出错,请重新输入";
}
// 关闭数据库连接
rs.close();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<h2>登录页面</h2>
<form action="login.jsp" method="post">
<label>用户名:</label> <input type="text" name="username"><br>
<label>密 码:</label> <input type="password" name="password"><br>
<input type="submit" value="登录"><br>
</form>
<p style="color: red;"><%=message%></p>
</body>
</html>
```
阅读全文