javaweb第12-00次上机: 用三层架构和mvc模式设计一个用户登录验证程序: 1、新建一个登录页面login.jsp,表单元素包含登录帐号和密码 2、新建一个UserDao接口和实现 类,用来访问数据库(调用DbUtils工具类API完成) 3、新建一个UserService接口和实现类,用来验证用户身份(调用 dao层) 4、新建一个控制器(Servlet),名为UsersController,用来验证身份,设帐号为test,密码为“test”时验证通过,当通过验证时在index.jsp页面上显示登录用户的名称
时间: 2023-12-06 09:44:25 浏览: 72
好的,这是一个比较典型的三层架构和MVC模式的应用程序设计,主要包括以下几个步骤:
1. 新建一个登录页面login.jsp,表单元素包含登录帐号和密码,并提交到UsersController控制器。
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form method="post" action="UsersController">
<label>Username:</label>
<input type="text" name="username" /><br/><br/>
<label>Password:</label>
<input type="password" name="password" /><br/><br/>
<input type="submit" value="Login" />
</form>
</body>
</html>
```
2. 新建一个UserDao接口和实现类,用来访问数据库(调用DbUtils工具类API完成)。
UserDao接口:
```java
public interface UserDao {
public User getUser(String username);
}
```
UserDao实现类:
```java
public class UserDaoImpl implements UserDao {
@Override
public User getUser(String username) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
User user = null;
try {
conn = DbUtils.getConnection();
String sql = "SELECT * FROM users WHERE username=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, username);
rs = stmt.executeQuery();
if (rs.next()) {
user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setName(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtils.close(rs, stmt, conn);
}
return user;
}
}
```
3. 新建一个UserService接口和实现类,用来验证用户身份(调用dao层)。
UserService接口:
```java
public interface UserService {
public boolean authenticate(String username, String password);
public String getName(String username);
}
```
UserService实现类:
```java
public class UserServiceImpl implements UserService {
private UserDao userDao = new UserDaoImpl();
@Override
public boolean authenticate(String username, String password) {
User user = userDao.getUser(username);
if (user == null) {
return false;
}
return user.getPassword().equals(password);
}
@Override
public String getName(String username) {
User user = userDao.getUser(username);
if (user != null) {
return user.getName();
}
return null;
}
}
```
4. 新建一个控制器(Servlet),名为UsersController,用来验证身份,设帐号为test,密码为“test”时验证通过,当通过验证时在index.jsp页面上显示登录用户的名称。
UsersController:
```java
public class UsersController extends HttpServlet {
private UserService userService = new UserServiceImpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
if (userService.authenticate(username, password)) {
request.getSession().setAttribute("username", username);
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
}
```
index.jsp:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index</title>
</head>
<body>
<h1>Welcome, <%= session.getAttribute("username") %>!</h1>
</body>
</html>
```
阅读全文