mvc登录实例mysql_MVC(JSP+JavaBean+Servlet)入口实例:用户登录验证,要有User、UserDao、UserController、login.jsp
时间: 2024-01-14 07:04:17 浏览: 85
以登陆例子讲解Jsp+servlet+javaBean框架实现MVC模式.docx
好的,我可以为您提供一个简单的 MVC 登录实例(JSP+JavaBean+Servlet+MySQL),包括 User、UserDao、UserController 和 login.jsp。请注意,这里的代码只是一个示例,需要根据您的具体需求进行调整。
首先,我们需要创建一个 User 类,用于保存用户信息:
```java
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
然后,我们需要创建一个 UserDao 类,用于处理与用户相关的数据库操作:
```java
public class UserDao {
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DB_URL = "jdbc:mysql://localhost:3306/mydb";
private static final String USER = "root";
private static final String PASS = "root";
public boolean isValid(User user) {
Connection conn = null;
PreparedStatement stmt = null;
boolean isValid = false;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "SELECT * FROM users WHERE username = ? AND password = ?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getPassword());
ResultSet rs = stmt.executeQuery();
isValid = rs.next();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return isValid;
}
}
```
接下来,我们需要创建一个 UserController 类,用于处理与用户相关的业务逻辑:
```java
@WebServlet("/login")
public class UserController extends HttpServlet {
private UserDao userDao;
@Override
public void init() throws ServletException {
super.init();
userDao = new UserDao();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User(username, password);
boolean isValid = userDao.isValid(user);
if (isValid) {
request.getSession().setAttribute("user", user);
response.sendRedirect("home.jsp");
} else {
request.setAttribute("error", "Invalid username or password");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
```
最后,我们需要创建一个 login.jsp 页面,用于展示登录表单:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<% String error = (String) request.getAttribute("error"); %>
<% if (error != null) { %>
<p><%= error %></p>
<% } %>
<form action="login" method="post">
<p>
<label>Username:</label>
<input type="text" name="username" required>
</p>
<p>
<label>Password:</label>
<input type="password" name="password" required>
</p>
<p>
<button type="submit">Login</button>
</p>
</form>
</body>
</html>
```
这就是一个简单的 MVC 登录实例,包括 User、UserDao、UserController 和 login.jsp。当用户在登录表单中输入用户名和密码后,系统会将其封装成一个 User 对象,然后通过 UserDao 进行数据库验证。如果验证通过,系统会将用户信息保存到 Session 中并跳转到 home.jsp 页面;否则,系统会返回登录表单并显示错误信息。
阅读全文