jsp+servlet实现增删改查
时间: 2023-08-31 13:12:55 浏览: 132
JSP和Servlet是Java Web编程中的两个重要技术,可以使用它们来实现增删改查功能。
首先,我们需要创建一个JavaBean类来表示我们要操作的数据。例如,我们创建一个名为"User"的JavaBean类,其中包含用户的ID、姓名、年龄、性别等属性,并提供相应的getter和setter方法。
接下来,我们需要编写一个Servlet来处理增删改查请求。在Servlet中,我们可以通过HttpServletRequest对象获取请求参数,并调用JavaBean的方法来操作数据。例如,我们可以根据请求参数来判断用户是要进行增加、删除、修改还是查询操作,然后调用相应的JavaBean方法来完成操作。
最后,我们需要编写一个JSP页面来展示数据和接收用户的请求。在JSP页面中,我们可以使用JSTL标签库和EL表达式来显示数据,并使用HTML表单来接收用户的请求参数。例如,我们可以使用<c:forEach>标签来遍历JavaBean中的数据,并使用<form>标签来提交用户的请求参数。
以下是一个简单的示例代码,实现了基本的增删改查功能:
JavaBean类User.java:
```
public class User {
private int id;
private String name;
private int age;
private String gender;
// getter和setter方法省略
}
```
Servlet类UserServlet.java:
```
public class UserServlet extends HttpServlet {
private List<User> userList = new ArrayList<>();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
action = "list";
}
switch (action) {
case "list":
listUsers(request, response);
break;
case "add":
addUser(request, response);
break;
case "edit":
editUser(request, response);
break;
case "delete":
deleteUser(request, response);
break;
default:
listUsers(request, response);
break;
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
private void listUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("userList", userList);
request.getRequestDispatcher("list.jsp").forward(request, response);
}
private void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
user.setGender(gender);
userList.add(user);
response.sendRedirect("user?action=list");
}
private void editUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
int age = Integer.parseInt(request.getParameter("age"));
String gender = request.getParameter("gender");
for (User user : userList) {
if (user.getId() == id) {
user.setName(name);
user.setAge(age);
user.setGender(gender);
break;
}
}
response.sendRedirect("user?action=list");
}
private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
for (User user : userList) {
if (user.getId() == id) {
userList.remove(user);
break;
}
}
response.sendRedirect("user?action=list");
}
}
```
JSP页面list.jsp:
```
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Gender</th>
<th>Action</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.gender}</td>
<td>
<a href="user?action=edit&id=${user.id}">Edit</a>
<a href="user?action=delete&id=${user.id}">Delete</a>
</td>
</tr>
</c:forEach>
</table>
<hr>
<h2>Add User</h2>
<form action="user" method="post">
<input type="hidden" name="action" value="add">
ID: <input type="text" name="id"><br>
Name: <input type="text" name="name"><br>
Age: <input type="text" name="age"><br>
Gender: <input type="radio" name="gender" value="M"> Male
<input type="radio" name="gender" value="F"> Female<br>
<input type="submit" value="Add">
</form>
</body>
</html>
```
在以上示例中,我们使用GET和POST方法来处理不同类型的请求,使用switch语句来根据请求参数分派请求,使用JavaBean类来表示用户数据,使用JSTL和EL来显示数据,使用HTML表单来接收用户请求参数。这是一个非常简单的示例,实际应用中可能需要更复杂的逻辑和更多的功能。
阅读全文