java中级之jsp impl实现删除功能

kimvuong89

New member
** #Java #JSP #删除 #中级 #教程 **

## Cách triển khai chức năng xóa trong JSP

Trong hướng dẫn này, chúng tôi sẽ học cách thực hiện xóa chức năng trong JSP.Chúng tôi sẽ tạo một ứng dụng web đơn giản với một biểu mẫu cho phép người dùng nhập ID của bản ghi mà họ muốn xóa.Khi người dùng gửi biểu mẫu, bản ghi sẽ bị xóa khỏi cơ sở dữ liệu.

### 1. Tạo một ứng dụng web mới

Để tạo một ứng dụng web mới, hãy mở IDE yêu thích của bạn và tạo một dự án Maven mới.Trong tệp pom.xml của dự án, hãy thêm các phụ thuộc sau:

`` `XML
<phụ thuộc>
<phụ thuộc>
<ProupId> javax.servlet </groupID>
<Storifactid> javax.servlet-API </artifactid>
<phiên bản> 4.0.1 </phiên bản>
<Phạm vi> Được cung cấp </phạm vi>
</phụ thuộc>
<phụ thuộc>
<ProupId> javax.servlet.jsp </groupID>
<Storifactid> javax.servlet.jsp- API </artifactid>
<phiên bản> 2.3.3 </phiên bản>
<Phạm vi> Được cung cấp </phạm vi>
</phụ thuộc>
<phụ thuộc>
<ProupId> org.apache.commons </groupID>
<PartifactID> Commons-DBCP2 </ArtifactID>
<phiên bản> 2.7.0 </phiên bản>
</phụ thuộc>
<phụ thuộc>
<ProupId> MySQL </groupID>
<StorifactID> MySQL-ConneNector-Java </ArtifactID>
<phiên bản> 8.0.25 </phiên bản>
</phụ thuộc>
</phụ thuộc>
`` `

### 2. Tạo bảng cơ sở dữ liệu

Chúng ta cần tạo một bảng cơ sở dữ liệu để lưu trữ các bản ghi mà chúng ta sẽ xóa.Trong MySQL, bạn có thể tạo một bảng bằng lệnh sau:

`` `SQL
Tạo người dùng bảng (
id int not null auto_increment,
Tên varchar (255) không phải null,
Email Varchar (255) không phải NULL,
Khóa chính (ID)
);
`` `

### 3. Tạo một servlet để xử lý yêu cầu xóa

Chúng ta cần tạo một servlet để xử lý yêu cầu xóa.Servlet sẽ nhận ID của bản ghi sẽ bị xóa khỏi các tham số yêu cầu và xóa bản ghi khỏi cơ sở dữ liệu.

Tạo một lớp Java mới có tên là `DeleteServlet` và thêm mã sau:

`` `java
nhập java.io.ioException;
nhập java.io.printwriter;
nhập java.sql.connection;
nhập java.sql.preparedstatement;
nhập java.sql.resultset;

nhập javax.servlet.servletexception;
nhập javax.servlet.annotation.webservlet;
nhập javax.servlet.http.httpservlet;
nhập javax.servlet.http.httpservletrequest;
nhập javax.servlet.http.httpservletresponse;

@WebServlet ("/xóa")
Lớp công khai Xóa

@Ghi đè
Void được bảo vệ doget (yêu cầu httpservletRequest, phản hồi httpservletresponse)
ném ServletException, ioException {
// Nhận ID của bản ghi bị xóa khỏi các tham số yêu cầu
int id = integer.parseInt (request.getParameter ("id"));

// Kết nối với cơ sở dữ liệu
Kết nối kết nối = getConnection ();

// Chuẩn bị tuyên bố xóa
Chuỗi SQL = "Xóa khỏi người dùng WHERE ID =?";
Tuyên bố đã chuẩn bị = Connection.PreParestatement (SQL);

// Đặt giá trị tham số
statement.setInt (1, id);

// thực hiện câu lệnh
statement.executeUpdate ();

// Đóng kết nối
kết nối.close ();

// Chuyển hướng người dùng đến trang danh sách
Trả lời.SendRedirect ("Danh sách");
}

Kết nối riêng getConnection () ném sqlexception {
// Tạo nhóm kết nối
ConnectionPool ConnectionPool = new ConnectionPool ();

// Nhận kết nối từ hồ bơi
Kết nối kết nối = ConnectionPool.GetConnection ();

// Trả về kết nối
Kết nối quay lại;
}
}
=======================================
**#Java #JSP #删除功能 #中级 #教程**

## How to Implement Delete Functionality in JSP

In this tutorial, we will learn how to implement delete functionality in JSP. We will create a simple web application with a form that allows users to enter the ID of a record they want to delete. When the user submits the form, the record will be deleted from the database.

### 1. Create a new web application

To create a new web application, open your favorite IDE and create a new Maven project. In the project's pom.xml file, add the following dependencies:

```xml
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>
```

### 2. Create a database table

We need to create a database table to store the records that we will be deleting. In MySQL, you can create a table using the following command:

```sql
CREATE TABLE users (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
```

### 3. Create a servlet to handle the delete request

We need to create a servlet to handle the delete request. The servlet will receive the ID of the record to be deleted from the request parameters and delete the record from the database.

Create a new Java class called `DeleteServlet` and add the following code:

```java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/delete")
public class DeleteServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Get the ID of the record to be deleted from the request parameters
int id = Integer.parseInt(request.getParameter("id"));

// Connect to the database
Connection connection = getConnection();

// Prepare the delete statement
String sql = "DELETE FROM users WHERE id = ?";
PreparedStatement statement = connection.prepareStatement(sql);

// Set the parameter value
statement.setInt(1, id);

// Execute the statement
statement.executeUpdate();

// Close the connection
connection.close();

// Redirect the user to the list page
response.sendRedirect("list");
}

private Connection getConnection() throws SQLException {
// Create a connection pool
ConnectionPool connectionPool = new ConnectionPool();

// Get a connection from the pool
Connection connection = connectionPool.getConnection();

// Return the connection
return connection;
}
}
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock