使用JSP技术,编写JSP页面,实现以下功能: (1)制作JSP文件,添加表单,用来收集用户个人信息。 (2)制作JavaBean,用来存放个人信息。 (3)添加Servlet,将收集的用户信息保存到数据库中。 (4)制作JSP文件,添加查询功能,显示用户信息。
时间: 2024-06-09 07:07:30 浏览: 47
好的,我可以为您提供一些参考代码和步骤。
首先,我们需要创建一个 JSP 文件,包含一个表单用来收集用户信息,例如:
```
<!DOCTYPE html>
<html>
<head>
<title>User Information Form</title>
</head>
<body>
<h1>User Information Form</h1>
<form method="post" action="SaveUserInfoServlet">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone"><br><br>
<label for="address">Address:</label>
<textarea id="address" name="address"></textarea><br><br>
<input type="submit" value="Save">
</form>
</body>
</html>
```
接着,我们需要创建一个 JavaBean 来存储用户信息,例如:
```
public class UserInfo {
private String name;
private String email;
private String phone;
private String address;
public UserInfo() {
}
public UserInfo(String name, String email, String phone, String address) {
this.name = name;
this.email = email;
this.phone = phone;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
```
然后,我们需要创建一个 Servlet 来处理表单数据并将其保存到数据库中,例如:
```
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SaveUserInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String jdbcURL = "jdbc:mysql://localhost:3306/test";
private String jdbcUsername = "root";
private String jdbcPassword = "password";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
String address = request.getParameter("address");
UserInfo userInfo = new UserInfo(name, email, phone, address);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword);
String sql = "INSERT INTO user_info (name, email, phone, address) VALUES (?, ?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, userInfo.getName());
statement.setString(2, userInfo.getEmail());
statement.setString(3, userInfo.getPhone());
statement.setString(4, userInfo.getAddress());
statement.executeUpdate();
statement.close();
connection.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
response.sendRedirect("ViewUserInfo.jsp");
}
}
```
最后,我们需要创建一个 JSP 文件来显示用户信息,例如:
```
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="UserInfo" %>
<!DOCTYPE html>
<html>
<head>
<title>User Information</title>
</head>
<body>
<h1>User Information</h1>
<table border="1">
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
</tr>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");
String sql = "SELECT * FROM user_info";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
List<UserInfo> userInfoList = new ArrayList<>();
while (resultSet.next()) {
UserInfo userInfo = new UserInfo();
userInfo.setName(resultSet.getString("name"));
userInfo.setEmail(resultSet.getString("email"));
userInfo.setPhone(resultSet.getString("phone"));
userInfo.setAddress(resultSet.getString("address"));
userInfoList.add(userInfo);
}
request.setAttribute("userInfoList", userInfoList);
resultSet.close();
statement.close();
connection.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
%>
<c:forEach var="userInfo" items="${userInfoList}">
<tr>
<td>${userInfo.name}</td>
<td>${userInfo.email}</td>
<td>${userInfo.phone}</td>
<td>${userInfo.address}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
注意,我们在 JSP 文件中使用了 JSTL 标签库来遍历用户信息列表。
以上就是使用 JSP 技术实现收集用户信息并保存到数据库中,然后查询并显示用户信息的步骤和代码示例。
阅读全文