编写一个登录页面,输入学号和姓名,在数据库中进行验证,如果验证通过则在另一个页面中显示顾客的姓名。要求使用javabean封装顾客消息,使用dao查询数据库
时间: 2024-02-28 12:53:17 浏览: 32
好的,这是一个比较详细的问题,需要按照步骤来解决。我会尽可能详细地给您讲解。
首先,我们需要创建一个JavaBean来封装顾客信息,包括学号和姓名。这个JavaBean可以包含以下代码:
```java
public class Customer {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
```
接下来,我们需要创建一个DAO类来查询数据库中的信息。这个DAO类可以包含以下代码:
```java
public class CustomerDAO {
public Customer find(String id, String name) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Customer customer = null;
try {
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM customer WHERE id=? AND name=?");
ps.setString(1, id);
ps.setString(2, name);
rs = ps.executeQuery();
if (rs.next()) {
customer = new Customer();
customer.setId(rs.getString("id"));
customer.setName(rs.getString("name"));
}
} finally {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (conn != null) conn.close();
}
return customer;
}
private Connection getConnection() throws SQLException {
// 这里可以根据需要选择不同的数据库连接方式
return DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
}
}
```
这个DAO类会根据输入的学号和姓名查询数据库中的信息,并返回一个封装了顾客信息的JavaBean对象。
接下来,我们需要创建一个登录页面,让用户输入学号和姓名。这个页面可以包含以下代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<form action="login" method="post">
<label>学号:</label>
<input type="text" name="id" /><br />
<label>姓名:</label>
<input type="text" name="name" /><br />
<input type="submit" value="登录" />
</form>
</body>
</html>
```
在这个页面中,用户需要输入学号和姓名,并点击登录按钮。当用户点击登录按钮时,页面会将输入的学号和姓名提交到服务器端的一个叫做“login”的Servlet中。
接下来,我们需要创建这个Servlet。这个Servlet可以包含以下代码:
```java
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String name = request.getParameter("name");
CustomerDAO dao = new CustomerDAO();
try {
Customer customer = dao.find(id, name);
if (customer != null) {
request.setAttribute("customer", customer);
request.getRequestDispatcher("/welcome.jsp").forward(request, response);
} else {
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
} catch (SQLException e) {
throw new ServletException(e);
}
}
}
```
在这个Servlet中,我们首先获取了用户输入的学号和姓名。然后,我们创建了一个CustomerDAO对象,并调用它的find()方法来查询数据库中的信息。如果查询到了顾客信息,我们就将这个信息放到request中,并转发到一个叫做“welcome.jsp”的页面中。在这个页面中,我们可以显示顾客的姓名。如果没有查询到信息,则重定向回登录页面。
最后,我们需要创建一个“welcome.jsp”页面来显示顾客的姓名。这个页面可以包含以下代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h1>欢迎您,<%= request.getAttribute("customer").getName() %>!</h1>
</body>
</html>
```
在这个页面中,我们使用了EL表达式来获取之前在Servlet中放到request中的顾客信息,并显示顾客的姓名。
完成了以上步骤,我们就可以在网页上创建一个登录页面,输入学号和姓名,在数据库中进行验证,如果验证通过则在另一个页面中显示顾客的姓名。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)